->Replace 2 spaces tabs by 4 spaces.

->fix the NULL and 0 pointer comparisons mixes
This commit is contained in:
ylecollen 2017-01-03 09:32:59 +01:00
parent b03cdebfb5
commit 65352d05aa
8 changed files with 369 additions and 369 deletions

View file

@ -335,7 +335,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
public OlmMessage encryptMessage(String aClearMsg) { public OlmMessage encryptMessage(String aClearMsg) {
OlmMessage encryptedMsgRetValue = new OlmMessage(); OlmMessage encryptedMsgRetValue = new OlmMessage();
if(0 != encryptMessageJni(aClearMsg, encryptedMsgRetValue)){ if (0 != encryptMessageJni(aClearMsg, encryptedMsgRetValue)){
encryptedMsgRetValue = null; encryptedMsgRetValue = null;
} }

View file

@ -19,7 +19,6 @@
using namespace AndroidOlmSdk; using namespace AndroidOlmSdk;
/** /**
* Init memory allocation for account creation. * Init memory allocation for account creation.
* @return valid memory allocation, NULL otherwise * @return valid memory allocation, NULL otherwise
@ -43,7 +42,6 @@ OlmAccount* initializeAccountMemory()
return accountPtr; return accountPtr;
} }
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz) JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz)
{ {
LOGD("## createNewAccountJni(): IN"); LOGD("## createNewAccountJni(): IN");
@ -53,7 +51,6 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject t
return (jlong)(intptr_t)accountPtr; return (jlong)(intptr_t)accountPtr;
} }
/** /**
* Release the account allocation made by initializeAccountMemory().<br> * Release the account allocation made by initializeAccountMemory().<br>
* This method MUST be called when java counter part account instance is done. * This method MUST be called when java counter part account instance is done.
@ -65,7 +62,7 @@ JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz
OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
if(!accountPtr) if (!accountPtr)
{ {
LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL"); LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL");
} }
@ -147,7 +144,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject
jbyteArray byteArrayRetValue = NULL; jbyteArray byteArrayRetValue = NULL;
OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz);
if (NULL == accountPtr) if (!accountPtr)
{ {
LOGE("## identityKeys(): failure - invalid Account ptr=NULL"); LOGE("## identityKeys(): failure - invalid Account ptr=NULL");
} }
@ -168,7 +165,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject
// retrieve key pairs in identityKeysBytesPtr // retrieve key pairs in identityKeysBytesPtr
size_t keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength); size_t keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength);
if(keysResult == olm_error()) if (keysResult == olm_error())
{ {
LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
} }
@ -177,7 +174,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject
// allocate the byte array to be returned to java // allocate the byte array to be returned to java
byteArrayRetValue = env->NewByteArray(identityKeysLength); byteArrayRetValue = env->NewByteArray(identityKeysLength);
if(NULL == byteArrayRetValue) if (!byteArrayRetValue)
{ {
LOGE("## identityKeys(): failure - return byte array OOM"); LOGE("## identityKeys(): failure - return byte array OOM");
} }
@ -243,7 +240,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject
uint8_t *randomBufferPtr = NULL; uint8_t *randomBufferPtr = NULL;
if ( (0!=randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength)) if ((0 != randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength))
{ {
LOGE("## generateOneTimeKeysJni(): failure - random buffer init"); LOGE("## generateOneTimeKeysJni(): failure - random buffer init");
} }
@ -304,7 +301,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t
{ {
// retrieve key pairs in keysBytesPtr // retrieve key pairs in keysBytesPtr
size_t keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); size_t keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength);
if(keysResult == olm_error()) { if (keysResult == olm_error()) {
LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
} }
else else
@ -388,7 +385,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env,
{ {
size_t result = olm_account_mark_keys_as_published(accountPtr); size_t result = olm_account_mark_keys_as_published(accountPtr);
if(result == olm_error()) if (result == olm_error())
{ {
LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr)); LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr));
retCode = ERROR_CODE_KO; retCode = ERROR_CODE_KO;
@ -417,7 +414,7 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz
{ {
LOGE("## signMessageJni(): failure - invalid aMessage param"); LOGE("## signMessageJni(): failure - invalid aMessage param");
} }
else if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) else if(!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz)))
{ {
LOGE("## signMessageJni(): failure - invalid account ptr"); LOGE("## signMessageJni(): failure - invalid account ptr");
} }
@ -490,23 +487,23 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid key"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid key");
} }
else if(!aErrorMsg) else if (!aErrorMsg)
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object");
} }
else if(!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) else if (!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid account ptr"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid account ptr");
} }
else if(!(errorMsgJClass = env->GetObjectClass(aErrorMsg))) else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class"); LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class");
} }
else if(!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID"); LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID");
} }
else if(!(keyPtr = env->GetStringUTFChars(aKey, 0))) else if (!(keyPtr = env->GetStringUTFChars(aKey, 0)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM"); LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM");
} }
@ -604,7 +601,7 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j
keyLength, keyLength,
(void*)pickledPtr, (void*)pickledPtr,
pickledLength); pickledLength);
if(result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_account_last_error(accountPtr); const char *errorMsgPtr = olm_account_last_error(accountPtr);
LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_account() Msg=%s",errorMsgPtr); LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_account() Msg=%s",errorMsgPtr);

View file

@ -26,28 +26,28 @@ using namespace AndroidOlmSdk;
*/ */
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)
{ {
OlmInboundGroupSession* sessionPtr = NULL; OlmInboundGroupSession* sessionPtr = NULL;
LOGD("## releaseSessionJni(): InBound group session IN"); LOGD("## releaseSessionJni(): InBound group session IN");
if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE("## releaseSessionJni(): failure - invalid inbound group session instance"); LOGE("## releaseSessionJni(): failure - invalid inbound group session instance");
} }
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));
#else #else
olm_clear_inbound_group_session(sessionPtr); olm_clear_inbound_group_session(sessionPtr);
#endif #endif
LOGD(" ## releaseSessionJni(): free IN"); LOGD(" ## releaseSessionJni(): free IN");
free(sessionPtr); free(sessionPtr);
LOGD(" ## releaseSessionJni(): free OUT"); LOGD(" ## releaseSessionJni(): free OUT");
} }
} }
/** /**
@ -64,18 +64,18 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *
LOGD("## createNewSessionJni(): inbound group session IN"); LOGD("## createNewSessionJni(): inbound group session IN");
sessionSize = olm_inbound_group_session_size(); sessionSize = olm_inbound_group_session_size();
if(0 == sessionSize) if (!sessionSize)
{ {
LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0"); LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0");
} }
else if(NULL != (sessionPtr=(OlmInboundGroupSession*)malloc(sessionSize))) else if ((sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize)))
{ {
sessionPtr = olm_inbound_group_session(sessionPtr); sessionPtr = olm_inbound_group_session(sessionPtr);
LOGD(" ## createNewSessionJni(): success - inbound group session size=%lu",static_cast<long unsigned int>(sessionSize)); LOGD(" ## createNewSessionJni(): success - inbound group session size=%lu",static_cast<long unsigned int>(sessionSize));
} }
else else
{ {
LOGE(" ## createNewSessionJni(): failure - inbound group session OOM"); LOGE(" ## createNewSessionJni(): failure - inbound group session OOM");
} }
return (jlong)(intptr_t)sessionPtr; return (jlong)(intptr_t)sessionPtr;
@ -95,15 +95,15 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes
LOGD("## initInboundGroupSessionWithSessionKeyJni(): inbound group session IN"); LOGD("## initInboundGroupSessionWithSessionKeyJni(): inbound group session IN");
if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid inbound group session instance"); LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid inbound group session instance");
} }
else if(0 == aSessionKey) else if (!aSessionKey)
{ {
LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"); LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey");
} }
else if(NULL == (sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0))) else if (!(sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0)))
{ {
LOGE(" ## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM"); LOGE(" ## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM");
} }
@ -113,7 +113,7 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes
LOGD(" ## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength)); LOGD(" ## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength));
sessionResult = olm_init_inbound_group_session(sessionPtr, sessionKeyPtr, sessionKeyLength); sessionResult = olm_init_inbound_group_session(sessionPtr, sessionKeyPtr, sessionKeyLength);
if(sessionResult == olm_error()) { if (sessionResult == olm_error()) {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); LOGE(" ## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr);
} }
@ -125,7 +125,7 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes
} }
// free local alloc // free local alloc
if(NULL!= sessionKeyPtr) if (sessionKeyPtr)
{ {
env->ReleaseStringUTFChars(aSessionKey, (const char*)sessionKeyPtr); env->ReleaseStringUTFChars(aSessionKey, (const char*)sessionKeyPtr);
} }
@ -140,12 +140,11 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
{ {
OlmInboundGroupSession *sessionPtr = NULL; OlmInboundGroupSession *sessionPtr = NULL;
uint8_t *sessionIdPtr = NULL;
jstring returnValueStr=0; jstring returnValueStr=0;
LOGD("## sessionIdentifierJni(): inbound group session IN"); LOGD("## sessionIdentifierJni(): inbound group session IN");
if(NULL == (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");
} }
@ -155,13 +154,16 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn
size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr); size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr);
LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
if(NULL == (sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t));
if (!sessionIdPtr)
{ {
LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"); LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM");
} }
else else
{ {
size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
if (result == olm_error()) if (result == olm_error())
{ {
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));
@ -402,7 +404,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JN
keyLength, keyLength,
(void*)pickledPtr, (void*)pickledPtr,
pickledLength); pickledLength);
if(result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_outbound_group_session() Msg=%s",errorMsgPtr); LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_outbound_group_session() Msg=%s",errorMsgPtr);

View file

@ -36,7 +36,7 @@ bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize)
{ {
LOGE("## setRandomInBuffer(): failure - aBuffer=NULL"); LOGE("## setRandomInBuffer(): failure - aBuffer=NULL");
} }
else if(!aRandomSize) else if (!aRandomSize)
{ {
LOGE("## setRandomInBuffer(): failure - random size=0"); LOGE("## setRandomInBuffer(): failure - random size=0");
} }
@ -113,7 +113,8 @@ bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize)
jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass) jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass)
{ {
jlong instanceId = 0; jlong instanceId = 0;
if (aJniEnv)
if (aJniEnv)
{ {
jclass requiredClass = aJniEnv->FindClass(aCallingClass); jclass requiredClass = aJniEnv->FindClass(aCallingClass);
jclass loaderClass = 0; jclass loaderClass = 0;
@ -227,7 +228,7 @@ jstring javaCStringToUtf8(JNIEnv *env, uint8_t *aCStringMsgPtr, size_t aMsgLengt
{ {
LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)"); LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)");
} }
else if (!(tempByteArray=env->NewByteArray(aMsgLength))) else if (!(tempByteArray = env->NewByteArray(aMsgLength)))
{ {
LOGE("## javaCStringToUtf8(): failure - return byte array OOM"); LOGE("## javaCStringToUtf8(): failure - return byte array OOM");
} }

View file

@ -21,15 +21,15 @@ using namespace AndroidOlmSdk;
JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersionJni)(JNIEnv* env, jobject thiz) JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersionJni)(JNIEnv* env, jobject thiz)
{ {
uint8_t majorVer=0, minorVer=0, patchVer=0; uint8_t majorVer=0, minorVer=0, patchVer=0;
jstring returnValueStr=0; jstring returnValueStr=0;
char buff[150]; char buff[150];
olm_get_library_version(&majorVer, &minorVer, &patchVer); olm_get_library_version(&majorVer, &minorVer, &patchVer);
LOGD("## getOlmLibVersionJni(): Major=%d Minor=%d Patch=%d", majorVer, minorVer, patchVer); LOGD("## getOlmLibVersionJni(): Major=%d Minor=%d Patch=%d", majorVer, minorVer, patchVer);
snprintf(buff, sizeof(buff), "%d.%d.%d", majorVer, minorVer, patchVer); snprintf(buff, sizeof(buff), "%d.%d.%d", majorVer, minorVer, patchVer);
returnValueStr = env->NewStringUTF((const char*)buff); returnValueStr = env->NewStringUTF((const char*)buff);
return returnValueStr; return returnValueStr;
} }

View file

@ -26,29 +26,29 @@ using namespace AndroidOlmSdk;
*/ */
JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz)
{ {
OlmOutboundGroupSession* sessionPtr = NULL; LOGD("## releaseSessionJni(): OutBound group session IN");
LOGD("## releaseSessionJni(): OutBound group session IN"); OlmOutboundGroupSession* sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if (!sessionPtr)
{ {
LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance"); LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance");
} }
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_outbound_group_session(sessionPtr); size_t retCode = olm_clear_outbound_group_session(sessionPtr);
LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",static_cast<long unsigned int>(retCode)); LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",static_cast<long unsigned int>(retCode));
#else #else
olm_clear_outbound_group_session(sessionPtr); olm_clear_outbound_group_session(sessionPtr);
#endif #endif
LOGD(" ## releaseSessionJni(): free IN"); LOGD(" ## releaseSessionJni(): free IN");
free(sessionPtr); free(sessionPtr);
LOGD(" ## releaseSessionJni(): free OUT"); LOGD(" ## releaseSessionJni(): free OUT");
} }
} }
/** /**
@ -65,18 +65,18 @@ JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv
LOGD("## createNewSessionJni(): outbound group session IN"); LOGD("## createNewSessionJni(): outbound group session IN");
sessionSize = olm_outbound_group_session_size(); sessionSize = olm_outbound_group_session_size();
if(0 == sessionSize) if (0 == sessionSize)
{ {
LOGE(" ## createNewSessionJni(): failure - outbound group session size = 0"); LOGE(" ## createNewSessionJni(): failure - outbound group session size = 0");
} }
else if(NULL != (sessionPtr=(OlmOutboundGroupSession*)malloc(sessionSize))) else if (!(sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize)))
{ {
sessionPtr = olm_outbound_group_session(sessionPtr); sessionPtr = olm_outbound_group_session(sessionPtr);
LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",static_cast<long unsigned int>(sessionSize)); LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",static_cast<long unsigned int>(sessionSize));
} }
else else
{ {
LOGE(" ## createNewSessionJni(): failure - outbound group session OOM"); LOGE(" ## createNewSessionJni(): failure - outbound group session OOM");
} }
return (jlong)(intptr_t)sessionPtr; return (jlong)(intptr_t)sessionPtr;
@ -89,12 +89,12 @@ JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv
JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz) JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz)
{ {
jint retCode = ERROR_CODE_KO; jint retCode = ERROR_CODE_KO;
OlmOutboundGroupSession *sessionPtr = NULL;
uint8_t *randomBuffPtr = NULL;
LOGD("## initOutboundGroupSessionJni(): IN"); LOGD("## initOutboundGroupSessionJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
if (!sessionPtr)
{ {
LOGE(" ## initOutboundGroupSessionJni(): failure - invalid outbound group session instance"); LOGE(" ## initOutboundGroupSessionJni(): failure - invalid outbound group session instance");
} }
@ -102,20 +102,24 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(
{ {
// compute random buffer // compute random buffer
size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr); size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr);
uint8_t *randomBuffPtr = NULL;
LOGW(" ## initOutboundGroupSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength)); LOGW(" ## initOutboundGroupSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength));
if((0!=randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
{ {
LOGE(" ## initOutboundGroupSessionJni(): failure - random buffer init"); LOGE(" ## initOutboundGroupSessionJni(): failure - random buffer init");
} }
else else
{ {
if(0==randomLength) if (0 == randomLength)
{ {
LOGW(" ## initOutboundGroupSessionJni(): random buffer is not required"); LOGW(" ## initOutboundGroupSessionJni(): random buffer is not required");
} }
size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength); size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength);
if(sessionResult == olm_error()) {
if (sessionResult == olm_error()) {
LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
} }
else else
@ -123,12 +127,9 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(
retCode = ERROR_CODE_OK; retCode = ERROR_CODE_OK;
LOGD(" ## initOutboundGroupSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); LOGD(" ## initOutboundGroupSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
} }
}
}
if(NULL != randomBuffPtr) free(randomBuffPtr);
{ }
free(randomBuffPtr);
} }
return retCode; return retCode;
@ -139,13 +140,11 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(
*/ */
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
{ {
OlmOutboundGroupSession *sessionPtr = NULL; LOGD("## sessionIdentifierJni(): outbound group session IN");
uint8_t *sessionIdPtr = NULL; OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
jstring returnValueStr=0; jstring returnValueStr=0;
LOGD("## sessionIdentifierJni(): outbound group session IN"); if (!sessionPtr)
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance"); LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance");
} }
@ -155,13 +154,16 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE
size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr); size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr);
LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
if(NULL == (sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t));
if (!sessionIdPtr)
{ {
LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM"); LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM");
} }
else else
{ {
size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
if (result == olm_error()) if (result == olm_error())
{ {
LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",reinterpret_cast<const char*>(olm_outbound_group_session_last_error(sessionPtr))); LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",reinterpret_cast<const char*>(olm_outbound_group_session_last_error(sessionPtr)));
@ -196,7 +198,7 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env,
LOGD("## messageIndexJni(): IN"); LOGD("## messageIndexJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance"); LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance");
} }
@ -204,24 +206,22 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env,
{ {
indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr)); indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr));
} }
LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue); LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue);
return indexRetValue; return indexRetValue;
} }
/** /**
* Get the base64-encoded current ratchet key for this session.<br> * Get the base64-encoded current ratchet key for this session.<br>
*/ */
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz) JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz)
{ {
OlmOutboundGroupSession *sessionPtr = NULL;
uint8_t *sessionKeyPtr = NULL;
jstring returnValueStr=0;
LOGD("## sessionKeyJni(): outbound group session IN"); LOGD("## sessionKeyJni(): outbound group session IN");
OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
jstring returnValueStr = 0;
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if (!sessionPtr)
{ {
LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance"); LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance");
} }
@ -231,13 +231,16 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env
size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr); size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr);
LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength)); LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength));
if(NULL == (sessionKeyPtr = (uint8_t*)malloc((sessionKeyLength+1)*sizeof(uint8_t)))) uint8_t *sessionKeyPtr = (uint8_t*)malloc((sessionKeyLength+1)*sizeof(uint8_t));
if (!sessionKeyPtr)
{ {
LOGE(" ## sessionKeyJni(): failure - session key allocation OOM"); LOGE(" ## sessionKeyJni(): failure - session key allocation OOM");
} }
else else
{ {
size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength); size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength);
if (result == olm_error()) if (result == olm_error())
{ {
LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
@ -261,22 +264,21 @@ 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 aClearMsg) JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsg)
{ {
LOGD("## encryptMessageJni(): IN");
jstring encryptedMsgRetValue = 0; jstring encryptedMsgRetValue = 0;
OlmOutboundGroupSession *sessionPtr = NULL; OlmOutboundGroupSession *sessionPtr = NULL;
const char *clearMsgPtr = NULL; const char *clearMsgPtr = NULL;
uint8_t *encryptedMsgPtr = NULL;
LOGD("## encryptMessageJni(): IN"); if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL"); LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL");
} }
else if(0 == aClearMsg) else if (!aClearMsg)
{ {
LOGE(" ## encryptMessageJni(): failure - invalid clear message"); LOGE(" ## encryptMessageJni(): failure - invalid clear message");
} }
else if(0 == (clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) else if (!(clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0)))
{ {
LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM"); LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM");
} }
@ -288,7 +290,9 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv
// compute max encrypted length // compute max encrypted length
size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength); size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength);
if(NULL == (encryptedMsgPtr = (uint8_t*)malloc((encryptedMsgLength+1)*sizeof(uint8_t)))) uint8_t *encryptedMsgPtr = (uint8_t*)malloc((encryptedMsgLength+1)*sizeof(uint8_t));
if (!encryptedMsgPtr)
{ {
LOGE(" ## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); LOGE(" ## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM");
} }
@ -301,7 +305,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv
clearMsgLength, clearMsgLength,
encryptedMsgPtr, encryptedMsgPtr,
encryptedMsgLength); encryptedMsgLength);
if(encryptedLength == olm_error()) if (encryptedLength == olm_error())
{ {
LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr)); LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
} }
@ -313,20 +317,17 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv
LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr)); LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr));
encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr); encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr);
} }
}
free(encryptedMsgPtr);
}
} }
// free alloc // free alloc
if(NULL != clearMsgPtr) if (clearMsgPtr)
{ {
env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr); env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr);
} }
if(NULL != encryptedMsgPtr)
{
free(encryptedMsgPtr);
}
return encryptedMsgRetValue; return encryptedMsgRetValue;
} }
@ -344,32 +345,31 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J
jmethodID errorMsgMethodId = 0; jmethodID errorMsgMethodId = 0;
jstring errorJstring = 0; jstring errorJstring = 0;
const char *keyPtr = NULL; const char *keyPtr = NULL;
void *pickledPtr = NULL;
OlmOutboundGroupSession* sessionPtr = NULL; OlmOutboundGroupSession* sessionPtr = NULL;
LOGD("## outbound group session serializeDataWithKeyJni(): IN"); LOGD("## outbound group session serializeDataWithKeyJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid session ptr"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid session ptr");
} }
else if(0 == aKey) else if (!aKey)
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid key"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid key");
} }
else if(0 == aErrorMsg) else if (!aErrorMsg)
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object");
} }
else if(0 == (errorMsgJClass = env->GetObjectClass(aErrorMsg))) else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class"); LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class");
} }
else if(0 == (errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID"); LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID");
} }
else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) else if (!(keyPtr = env->GetStringUTFChars(aKey, 0)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM"); LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM");
} }
@ -380,7 +380,9 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J
LOGD(" ## serializeDataWithKeyJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); LOGD(" ## serializeDataWithKeyJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
LOGD(" ## serializeDataWithKeyJni(): key=%s",(char const *)keyPtr); LOGD(" ## serializeDataWithKeyJni(): key=%s",(char const *)keyPtr);
if(NULL == (pickledPtr = (void*)malloc((pickledLength+1)*sizeof(uint8_t)))) void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t));
if(!pickledPtr)
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - pickledPtr buffer OOM"); LOGE(" ## serializeDataWithKeyJni(): failure - pickledPtr buffer OOM");
} }
@ -391,12 +393,12 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J
keyLength, keyLength,
(void*)pickledPtr, (void*)pickledPtr,
pickledLength); pickledLength);
if(result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_outbound_group_session() Msg=%s",errorMsgPtr); LOGE(" ## serializeDataWithKeyJni(): failure - olm_pickle_outbound_group_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);
} }
@ -409,17 +411,14 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(J
LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr)); LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
} }
} }
free(pickledPtr);
} }
// free alloc // free alloc
if(NULL != keyPtr) if (keyPtr)
{ {
env->ReleaseStringUTFChars(aKey, keyPtr); env->ReleaseStringUTFChars(aKey, keyPtr);
}
if(NULL != pickledPtr)
{
free(pickledPtr);
} }
return pickledDataRetValue; return pickledDataRetValue;
@ -435,23 +434,23 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)
LOGD("## initWithSerializedDataJni(): IN"); LOGD("## initWithSerializedDataJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## initWithSerializedDataJni(): failure - session failure OOM"); LOGE(" ## initWithSerializedDataJni(): failure - session failure OOM");
} }
else if(0 == aKey) else if (!aKey)
{ {
LOGE(" ## initWithSerializedDataJni(): failure - invalid key"); LOGE(" ## initWithSerializedDataJni(): failure - invalid key");
} }
else if(0 == aSerializedData) else if (!aSerializedData)
{ {
LOGE(" ## initWithSerializedDataJni(): failure - serialized data"); LOGE(" ## initWithSerializedDataJni(): failure - serialized data");
} }
else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) else if (!(keyPtr = env->GetStringUTFChars(aKey, 0)))
{ {
LOGE(" ## initWithSerializedDataJni(): failure - keyPtr JNI allocation OOM"); LOGE(" ## initWithSerializedDataJni(): failure - keyPtr JNI allocation OOM");
} }
else if(NULL == (pickledPtr = env->GetStringUTFChars(aSerializedData, 0))) else if (!(pickledPtr = env->GetStringUTFChars(aSerializedData, 0)))
{ {
LOGE(" ## initWithSerializedDataJni(): failure - pickledPtr JNI allocation OOM"); LOGE(" ## initWithSerializedDataJni(): failure - pickledPtr JNI allocation OOM");
} }
@ -468,7 +467,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)
keyLength, keyLength,
(void*)pickledPtr, (void*)pickledPtr,
pickledLength); pickledLength);
if(result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_outbound_group_session() Msg=%s",errorMsgPtr); LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_outbound_group_session() Msg=%s",errorMsgPtr);
@ -481,12 +480,12 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)
} }
// free alloc // free alloc
if(NULL != keyPtr) if (keyPtr)
{ {
env->ReleaseStringUTFChars(aKey, keyPtr); env->ReleaseStringUTFChars(aKey, keyPtr);
} }
if(NULL != pickledPtr) if (pickledPtr)
{ {
env->ReleaseStringUTFChars(aSerializedData, pickledPtr); env->ReleaseStringUTFChars(aSerializedData, pickledPtr);
} }

View file

@ -26,17 +26,18 @@ using namespace AndroidOlmSdk;
**/ **/
OlmSession* initializeSessionMemory() OlmSession* initializeSessionMemory()
{ {
OlmSession* sessionPtr = NULL;
size_t sessionSize = olm_session_size(); size_t sessionSize = olm_session_size();
OlmSession* sessionPtr = (OlmSession*)malloc(sessionSize);
if(NULL != (sessionPtr=(OlmSession*)malloc(sessionSize))) if (sessionPtr)
{ // init session object {
sessionPtr = olm_session(sessionPtr); // init session object
LOGD("## initializeSessionMemory(): success - OLM session size=%lu",static_cast<long unsigned int>(sessionSize)); sessionPtr = olm_session(sessionPtr);
LOGD("## initializeSessionMemory(): success - OLM session size=%lu",static_cast<long unsigned int>(sessionSize));
} }
else else
{ {
LOGE("## initializeSessionMemory(): failure - OOM"); LOGE("## initializeSessionMemory(): failure - OOM");
} }
return sessionPtr; return sessionPtr;
@ -53,21 +54,20 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject t
JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz)
{ {
OlmSession* sessionPtr = NULL; LOGD("## releaseSessionJni(): IN");
OlmSession* sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz);
LOGD("## releaseSessionJni(): IN"); if (!sessionPtr)
{
LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL");
}
else
{
olm_clear_session(sessionPtr);
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) // even if free(NULL) does not crash, logs are performed for debug purpose
{ free(sessionPtr);
LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL"); }
}
else
{
olm_clear_session(sessionPtr);
// even if free(NULL) does not crash, logs are performed for debug purpose
free(sessionPtr);
}
} }
/** /**
@ -78,18 +78,18 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz
**/ **/
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz)
{ {
OlmSession* sessionPtr = NULL;
LOGD("## initNewSessionJni(): OlmSession IN"); LOGD("## initNewSessionJni(): OlmSession IN");
OlmSession* sessionPtr = initializeSessionMemory();
// init account memory allocation // init account memory allocation
if(NULL == (sessionPtr = initializeSessionMemory())) if (!sessionPtr)
{ {
LOGE(" ## initNewSessionJni(): failure - init session OOM"); LOGE(" ## initNewSessionJni(): failure - init session OOM");
} }
else else
{ {
LOGD(" ## initNewSessionJni(): success - OLM session created"); LOGD(" ## initNewSessionJni(): success - OLM session created");
} }
return (jlong)(intptr_t)sessionPtr; return (jlong)(intptr_t)sessionPtr;
@ -112,38 +112,41 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
jint retCode = ERROR_CODE_KO; jint retCode = ERROR_CODE_KO;
OlmSession* sessionPtr = NULL; OlmSession* sessionPtr = NULL;
OlmAccount* accountPtr = NULL; OlmAccount* accountPtr = NULL;
const char* theirIdentityKeyPtr = NULL;
const char* theirOneTimeKeyPtr = NULL;
uint8_t *randomBuffPtr = NULL;
size_t sessionResult;
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## initOutboundSessionJni(): failure - invalid Session ptr=NULL"); LOGE("## initOutboundSessionJni(): failure - invalid Session ptr=NULL");
} }
else if(NULL == (accountPtr = (OlmAccount*)aOlmAccountId)) else if (!(accountPtr = (OlmAccount*)aOlmAccountId))
{ {
LOGE("## initOutboundSessionJni(): failure - invalid Account ptr=NULL"); LOGE("## initOutboundSessionJni(): failure - invalid Account ptr=NULL");
} }
else if((0==aTheirIdentityKey) || (0==aTheirOneTimeKey)) else if (!aTheirIdentityKey || !aTheirOneTimeKey)
{ {
LOGE("## initOutboundSessionJni(): failure - invalid keys"); LOGE("## initOutboundSessionJni(): failure - invalid keys");
} }
else else
{ // allocate random buffer {
size_t randomSize = olm_create_outbound_session_random_length(sessionPtr); size_t randomSize = olm_create_outbound_session_random_length(sessionPtr);
uint8_t *randomBuffPtr = NULL;
LOGD("## initOutboundSessionJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize)); LOGD("## initOutboundSessionJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize));
if((0!=randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
if ( (0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
{ {
LOGE("## initOutboundSessionJni(): failure - random buffer init"); LOGE("## initOutboundSessionJni(): failure - random buffer init");
} }
else else
{ // convert identity & one time keys to C strings {
if(NULL == (theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) const char* theirIdentityKeyPtr = NULL;
const char* theirOneTimeKeyPtr = NULL;
// convert identity & one time keys to C strings
if (!(theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0)))
{ {
LOGE("## initOutboundSessionJni(): failure - identityKey JNI allocation OOM"); LOGE("## initOutboundSessionJni(): failure - identityKey JNI allocation OOM");
} }
else if(NULL == (theirOneTimeKeyPtr = env->GetStringUTFChars(aTheirOneTimeKey, 0))) else if (!(theirOneTimeKeyPtr = env->GetStringUTFChars(aTheirOneTimeKey, 0)))
{ {
LOGE("## initOutboundSessionJni(): failure - one time Key JNI allocation OOM"); LOGE("## initOutboundSessionJni(): failure - one time Key JNI allocation OOM");
} }
@ -153,7 +156,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
size_t theirOneTimeKeyLength = (size_t)env->GetStringUTFLength(aTheirOneTimeKey); size_t theirOneTimeKeyLength = (size_t)env->GetStringUTFLength(aTheirOneTimeKey);
LOGD("## initOutboundSessionJni(): identityKey=%s oneTimeKey=%s",theirIdentityKeyPtr,theirOneTimeKeyPtr); LOGD("## initOutboundSessionJni(): identityKey=%s oneTimeKey=%s",theirIdentityKeyPtr,theirOneTimeKeyPtr);
sessionResult = olm_create_outbound_session(sessionPtr, size_t sessionResult = olm_create_outbound_session(sessionPtr,
accountPtr, accountPtr,
theirIdentityKeyPtr, theirIdentityKeyPtr,
theirIdentityKeyLength, theirIdentityKeyLength,
@ -161,7 +164,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
theirOneTimeKeyLength, theirOneTimeKeyLength,
(void*)randomBuffPtr, (void*)randomBuffPtr,
randomSize); randomSize);
if(sessionResult == olm_error()) { if (sessionResult == olm_error()) {
LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
else else
@ -170,25 +173,24 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
LOGD("## initOutboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); LOGD("## initOutboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
} }
} }
if (theirIdentityKeyPtr)
{
env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr);
}
if (theirOneTimeKeyPtr)
{
env->ReleaseStringUTFChars(aTheirOneTimeKey, theirOneTimeKeyPtr);
}
if (randomBuffPtr)
{
free(randomBuffPtr);
}
} }
} }
// **** free mem alloc ***
if(NULL!= randomBuffPtr)
{
free(randomBuffPtr);
}
if(NULL!= theirIdentityKeyPtr)
{
env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr);
}
if(NULL!= theirOneTimeKeyPtr)
{
env->ReleaseStringUTFChars(aTheirOneTimeKey, theirOneTimeKeyPtr);
}
return retCode; return retCode;
} }
@ -208,24 +210,25 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject
jint retCode = ERROR_CODE_KO; jint retCode = ERROR_CODE_KO;
OlmSession *sessionPtr = NULL; OlmSession *sessionPtr = NULL;
OlmAccount *accountPtr = NULL; OlmAccount *accountPtr = NULL;
const char *messagePtr = NULL;
size_t sessionResult; size_t sessionResult;
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## initInboundSessionJni(): failure - invalid Session ptr=NULL"); LOGE("## initInboundSessionJni(): failure - invalid Session ptr=NULL");
} }
else if(NULL == (accountPtr = (OlmAccount*)aOlmAccountId)) else if (!(accountPtr = (OlmAccount*)aOlmAccountId))
{ {
LOGE("## initInboundSessionJni(): failure - invalid Account ptr=NULL"); LOGE("## initInboundSessionJni(): failure - invalid Account ptr=NULL");
} }
else if(0==aOneTimeKeyMsg) else if (!aOneTimeKeyMsg)
{ {
LOGE("## initInboundSessionJni(): failure - invalid message"); LOGE("## initInboundSessionJni(): failure - invalid message");
} }
else else
{ // convert message to C strings {
if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) const char *messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0);
if (!messagePtr)
{ {
LOGE("## initInboundSessionJni(): failure - message JNI allocation OOM"); LOGE("## initInboundSessionJni(): failure - message JNI allocation OOM");
} }
@ -235,7 +238,9 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject
LOGD("## initInboundSessionJni(): messageLength=%lu message=%s", static_cast<long unsigned int>(messageLength), messagePtr); LOGD("## initInboundSessionJni(): messageLength=%lu message=%s", static_cast<long unsigned int>(messageLength), messagePtr);
sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength); sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength);
if(sessionResult == olm_error()) {
if (sessionResult == olm_error())
{
LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
else else
@ -268,27 +273,27 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env,
const char *theirIdentityKeyPtr = NULL; const char *theirIdentityKeyPtr = NULL;
size_t sessionResult; size_t sessionResult;
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL"); LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL");
} }
else if(NULL == (accountPtr = (OlmAccount*)aOlmAccountId)) else if (!(accountPtr = (OlmAccount*)aOlmAccountId))
{ {
LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Account ptr=NULL"); LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Account ptr=NULL");
} }
else if(0 == aTheirIdentityKey) else if (!aTheirIdentityKey)
{ {
LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey"); LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey");
} }
else if(0==aOneTimeKeyMsg) else if (!aOneTimeKeyMsg)
{ {
LOGE("## initInboundSessionJni(): failure - invalid one time key message"); LOGE("## initInboundSessionJni(): failure - invalid one time key message");
} }
else if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) else if (!(messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0)))
{ {
LOGE("## initInboundSessionFromIdKeyJni(): failure - message JNI allocation OOM"); LOGE("## initInboundSessionFromIdKeyJni(): failure - message JNI allocation OOM");
} }
else if(NULL == (theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) else if(!(theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0)))
{ {
LOGE("## initInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM"); LOGE("## initInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM");
} }
@ -300,7 +305,8 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env,
LOGD("## initInboundSessionFromIdKeyJni(): message=%s messageLength=%lu",messagePtr,static_cast<long unsigned int>(messageLength)); LOGD("## initInboundSessionFromIdKeyJni(): message=%s messageLength=%lu",messagePtr,static_cast<long unsigned int>(messageLength));
sessionResult = olm_create_inbound_session_from(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, (void*)messagePtr , messageLength); sessionResult = olm_create_inbound_session_from(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, (void*)messagePtr , messageLength);
if(sessionResult == olm_error()) { if (sessionResult == olm_error())
{
LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
else else
@ -311,11 +317,12 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env,
} }
// free local alloc // free local alloc
if(NULL!= messagePtr) if (messagePtr)
{ {
env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr);
} }
if(NULL!= theirIdentityKeyPtr)
if (theirIdentityKeyPtr)
{ {
env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr); env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr);
} }
@ -335,15 +342,15 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje
OlmSession *sessionPtr = NULL; OlmSession *sessionPtr = NULL;
const char *messagePtr = NULL; const char *messagePtr = NULL;
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## matchesInboundSessionJni(): failure - invalid Session ptr=NULL"); LOGE("## matchesInboundSessionJni(): failure - invalid Session ptr=NULL");
} }
else if(0==aOneTimeKeyMsg) else if (!aOneTimeKeyMsg)
{ {
LOGE("## matchesInboundSessionJni(): failure - invalid one time key message"); LOGE("## matchesInboundSessionJni(): failure - invalid one time key message");
} }
else if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) else if (!(messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0)))
{ {
LOGE("## matchesInboundSessionJni(): failure - one time key JNI allocation OOM"); LOGE("## matchesInboundSessionJni(): failure - one time key JNI allocation OOM");
} }
@ -354,7 +361,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje
size_t matchResult = olm_matches_inbound_session(sessionPtr, (void*)messagePtr , messageLength); size_t matchResult = olm_matches_inbound_session(sessionPtr, (void*)messagePtr , messageLength);
//if(matchResult == olm_error()) { //if(matchResult == olm_error()) {
// for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0 // for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0
if(matchResult != 1) { if (matchResult != 1) {
LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
else else
@ -365,9 +372,9 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje
} }
// free local alloc // free local alloc
if(NULL!= messagePtr) if (messagePtr)
{ {
env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr);
} }
return retCode; return retCode;
@ -388,23 +395,23 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J
const char *messagePtr = NULL; const char *messagePtr = NULL;
const char *theirIdentityKeyPtr = NULL; const char *theirIdentityKeyPtr = NULL;
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL"); LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL");
} }
else if(0 == aTheirIdentityKey) else if (!aTheirIdentityKey)
{ {
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey"); LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey");
} }
else if(NULL == (theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0))) else if (!(theirIdentityKeyPtr = env->GetStringUTFChars(aTheirIdentityKey, 0)))
{ {
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM"); LOGE("## matchesInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM");
} }
else if(0==aOneTimeKeyMsg) else if (!aOneTimeKeyMsg)
{ {
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid one time key message"); LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid one time key message");
} }
else if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) else if (!(messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0)))
{ {
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - one time key JNI allocation OOM"); LOGE("## matchesInboundSessionFromIdKeyJni(): failure - one time key JNI allocation OOM");
} }
@ -412,11 +419,12 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J
{ {
size_t identityKeyLength = (size_t)env->GetStringUTFLength(aTheirIdentityKey); size_t identityKeyLength = (size_t)env->GetStringUTFLength(aTheirIdentityKey);
size_t messageLength = (size_t)env->GetStringUTFLength(aOneTimeKeyMsg); size_t messageLength = (size_t)env->GetStringUTFLength(aOneTimeKeyMsg);
size_t matchResult = olm_matches_inbound_session_from(sessionPtr, (void const *)theirIdentityKeyPtr, identityKeyLength, (void*)messagePtr , messageLength); size_t matchResult = olm_matches_inbound_session_from(sessionPtr, (void const *)theirIdentityKeyPtr, identityKeyLength, (void*)messagePtr , messageLength);
//if(matchResult == olm_error()) { //if(matchResult == olm_error()) {
// for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0 // for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0
if(matchResult != 1) { if (matchResult != 1)
{
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
else else
@ -427,14 +435,14 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J
} }
// free local alloc // free local alloc
if(NULL!= theirIdentityKeyPtr) if (theirIdentityKeyPtr)
{ {
env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr); env->ReleaseStringUTFChars(aTheirIdentityKey, theirIdentityKeyPtr);
} }
if(NULL!= messagePtr) if (messagePtr)
{ {
env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr); env->ReleaseStringUTFChars(aOneTimeKeyMsg, messagePtr);
} }
return retCode; return retCode;
@ -452,39 +460,37 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
jint retCode = ERROR_CODE_KO; jint retCode = ERROR_CODE_KO;
OlmSession *sessionPtr = NULL; OlmSession *sessionPtr = NULL;
const char *clearMsgPtr = NULL; const char *clearMsgPtr = NULL;
uint8_t *randomBuffPtr = NULL;
void *encryptedMsgPtr = NULL;
jclass encryptedMsgJClass = 0; jclass encryptedMsgJClass = 0;
jfieldID encryptedMsgFieldId; jfieldID encryptedMsgFieldId;
jfieldID typeMsgFieldId; jfieldID typeMsgFieldId;
LOGD("## encryptMessageJni(): IN "); LOGD("## encryptMessageJni(): IN ");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL"); LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL");
} }
else if(0 == aClearMsg) else if (!aClearMsg)
{ {
LOGE("## encryptMessageJni(): failure - invalid clear message"); LOGE("## encryptMessageJni(): failure - invalid clear message");
} }
else if(0 == aEncryptedMsg) else if (!aEncryptedMsg)
{ {
LOGE("## encryptMessageJni(): failure - invalid encrypted message"); LOGE("## encryptMessageJni(): failure - invalid encrypted message");
} }
else if(NULL == (clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) else if (!(clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0)))
{ {
LOGE("## encryptMessageJni(): failure - clear message JNI allocation OOM"); LOGE("## encryptMessageJni(): failure - clear message JNI allocation OOM");
} }
else if(0 == (encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg)))
{ {
LOGE("## encryptMessageJni(): failure - unable to get crypted message class"); LOGE("## encryptMessageJni(): failure - unable to get crypted message class");
} }
else if(0 == (encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;")))
{ {
LOGE("## encryptMessageJni(): failure - unable to get message field"); LOGE("## encryptMessageJni(): failure - unable to get message field");
} }
else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J")))
{ {
LOGE("## encryptMessageJni(): failure - unable to get message type field"); LOGE("## encryptMessageJni(): failure - unable to get message type field");
} }
@ -492,13 +498,16 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
{ {
// get message type // get message type
size_t messageType = olm_encrypt_message_type(sessionPtr); size_t messageType = olm_encrypt_message_type(sessionPtr);
uint8_t *randomBuffPtr = NULL;
// compute random buffer // compute random buffer
// Note: olm_encrypt_random_length() can return 0, which means // Note: olm_encrypt_random_length() can return 0, which means
// it just does not need new random data to encrypt a new message // it just does not need new random data to encrypt a new message
size_t randomLength = olm_encrypt_random_length(sessionPtr); size_t randomLength = olm_encrypt_random_length(sessionPtr);
LOGD("## encryptMessageJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength)); LOGD("## encryptMessageJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength));
if((0!=randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
{ {
LOGE("## encryptMessageJni(): failure - random buffer init"); LOGE("## encryptMessageJni(): failure - random buffer init");
} }
@ -507,13 +516,16 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
// alloc buffer for encrypted message // alloc buffer for encrypted message
size_t clearMsgLength = (size_t)env->GetStringUTFLength(aClearMsg); size_t clearMsgLength = (size_t)env->GetStringUTFLength(aClearMsg);
size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength); size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength);
if(NULL == (encryptedMsgPtr = (void*)malloc((encryptedMsgLength+1)*sizeof(uint8_t))))
void *encryptedMsgPtr = malloc((encryptedMsgLength+1)*sizeof(uint8_t));
if (!encryptedMsgPtr)
{ {
LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM");
} }
else else
{ {
if(0==randomLength) if (0 == randomLength)
{ {
LOGW("## encryptMessageJni(): random buffer is not required"); LOGW("## encryptMessageJni(): random buffer is not required");
} }
@ -527,7 +539,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
randomLength, randomLength,
encryptedMsgPtr, encryptedMsgPtr,
encryptedMsgLength); encryptedMsgLength);
if(result == olm_error()) if (result == olm_error())
{ {
LOGE("## encryptMessageJni(): failure - Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## encryptMessageJni(): failure - Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
@ -546,30 +558,23 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
retCode = ERROR_CODE_OK; retCode = ERROR_CODE_OK;
LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg=%s", static_cast<long unsigned int>(result), static_cast<long unsigned int>(messageType), static_cast<long unsigned int>((size_t)env->GetStringUTFLength(encryptedJstring)), (const char*)encryptedMsgPtr); LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg=%s", static_cast<long unsigned int>(result), static_cast<long unsigned int>(messageType), static_cast<long unsigned int>((size_t)env->GetStringUTFLength(encryptedJstring)), (const char*)encryptedMsgPtr);
} }
free(encryptedMsgPtr);
} }
free(randomBuffPtr);
} }
} }
// free alloc // free alloc
if(NULL != clearMsgPtr) if (clearMsgPtr)
{ {
env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr); env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr);
}
if(NULL != randomBuffPtr)
{
free(randomBuffPtr);
}
if(NULL != encryptedMsgPtr)
{
free(encryptedMsgPtr);
} }
return retCode; return retCode;
} }
/** /**
* Decrypt a message using the session.<br> * Decrypt a message using the session.<br>
* @param aEncryptedMsg message to decrypt * @param aEncryptedMsg message to decrypt
@ -591,31 +596,31 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t
LOGD("## decryptMessageJni(): IN - OlmSession"); LOGD("## decryptMessageJni(): IN - OlmSession");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL"); LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL");
} }
else if(0 == aEncryptedMsg) else if (!aEncryptedMsg)
{ {
LOGE("## decryptMessageJni(): failure - invalid encrypted message"); LOGE("## decryptMessageJni(): failure - invalid encrypted message");
} }
else if(0 == (encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg)))
{ {
LOGE("## decryptMessageJni(): failure - unable to get encrypted message class"); LOGE("## decryptMessageJni(): failure - unable to get encrypted message class");
} }
else if(0 == (encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;")))
{ {
LOGE("## decryptMessageJni(): failure - unable to get message field"); LOGE("## decryptMessageJni(): failure - unable to get message field");
} }
else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J")))
{ {
LOGE("## decryptMessageJni(): failure - unable to get message type field"); LOGE("## decryptMessageJni(): failure - unable to get message type field");
} }
else if(0 == (encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId))) else if (!(encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId)))
{ {
LOGE("## decryptMessageJni(): failure - JNI encrypted object "); LOGE("## decryptMessageJni(): failure - JNI encrypted object ");
} }
else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0))) else if (!(encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0)))
{ {
LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM");
} }
@ -638,7 +643,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t
encryptedMsgLength); encryptedMsgLength);
// Note: tempEncryptedPtr is destroyed by olm_decrypt_max_plaintext_length() // Note: tempEncryptedPtr is destroyed by olm_decrypt_max_plaintext_length()
if(maxPlainTextLength == olm_error()) if (maxPlainTextLength == olm_error())
{ {
LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
@ -657,14 +662,14 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t
encryptedMsgLength, encryptedMsgLength,
plainTextMsgPtr, plainTextMsgPtr,
maxPlainTextLength); maxPlainTextLength);
if(plaintextLength == olm_error()) if (plaintextLength == olm_error())
{ {
LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",(const char *)olm_session_last_error(sessionPtr)); LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",(const char *)olm_session_last_error(sessionPtr));
} }
else else
{ {
// UTF-8 conversion workaround for issue on Android versions older than Marshmallow (23) // UTF-8 conversion workaround for issue on Android versions older than Marshmallow (23)
if(aIsUtf8ConversionRequired) if (aIsUtf8ConversionRequired)
{ {
decryptedMsgRetValue = javaCStringToUtf8(env, plainTextMsgPtr, plaintextLength); decryptedMsgRetValue = javaCStringToUtf8(env, plainTextMsgPtr, plaintextLength);
if(0 == decryptedMsgRetValue) if(0 == decryptedMsgRetValue)
@ -689,17 +694,17 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t
} }
// free alloc // free alloc
if(NULL != encryptedMsgPtr) if (encryptedMsgPtr)
{ {
env->ReleaseStringUTFChars(encryptedMsgJstring, encryptedMsgPtr); env->ReleaseStringUTFChars(encryptedMsgJstring, encryptedMsgPtr);
} }
if(NULL != tempEncryptedPtr) if (tempEncryptedPtr)
{ {
free(tempEncryptedPtr); free(tempEncryptedPtr);
} }
if(NULL != plainTextMsgPtr) if (plainTextMsgPtr)
{ {
free(plainTextMsgPtr); free(plainTextMsgPtr);
} }
@ -714,47 +719,49 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t
*/ */
JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz) JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz)
{ {
OlmSession *sessionPtr = NULL; jstring returnValueStr=0;
void *sessionIdPtr = NULL;
jstring returnValueStr=0;
LOGD("## getSessionIdentifierJni(): IN "); LOGD("## getSessionIdentifierJni(): IN ");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) OlmSession *sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz);
{
LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL");
}
else
{
// get the size to alloc to contain the id
size_t lengthSessionId = olm_session_id_length(sessionPtr);
LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
if(NULL == (sessionIdPtr = (void*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) if (!sessionPtr)
{ {
LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM"); LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL");
} }
else else
{ {
size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId); // get the size to alloc to contain the id
size_t lengthSessionId = olm_session_id_length(sessionPtr);
LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
if (result == olm_error()) void *sessionIdPtr = malloc((lengthSessionId+1)*sizeof(uint8_t));
{
LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",(const char *)olm_session_last_error(sessionPtr));
}
else
{
// update length
(static_cast<char*>(sessionIdPtr))[result] = static_cast<char>('\0');
LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr); if (!sessionIdPtr)
returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); {
} LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM");
free(sessionIdPtr); }
} else
} {
size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
return returnValueStr; if (result == olm_error())
{
LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",(const char *)olm_session_last_error(sessionPtr));
}
else
{
// update length
(static_cast<char*>(sessionIdPtr))[result] = static_cast<char>('\0');
LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
returnValueStr = env->NewStringUTF((const char*)sessionIdPtr);
}
free(sessionIdPtr);
}
}
return returnValueStr;
} }
@ -771,32 +778,31 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job
jmethodID errorMsgMethodId = 0; jmethodID errorMsgMethodId = 0;
jstring errorJstring = 0; jstring errorJstring = 0;
const char *keyPtr = NULL; const char *keyPtr = NULL;
void *pickledPtr = NULL;
OlmSession* sessionPtr = NULL; OlmSession* sessionPtr = NULL;
LOGD("## serializeDataWithKeyJni(): IN"); LOGD("## serializeDataWithKeyJni(): IN");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid session ptr"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid session ptr");
} }
else if(0 == aKey) else if (!aKey)
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid key"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid key");
} }
else if(0 == aErrorMsg) else if (!aErrorMsg)
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object"); LOGE(" ## serializeDataWithKeyJni(): failure - invalid error object");
} }
else if(0 == (errorMsgJClass = env->GetObjectClass(aErrorMsg))) else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class"); LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error class");
} }
else if(0 == (errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID"); LOGE(" ## serializeDataWithKeyJni(): failure - unable to get error method ID");
} }
else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) else if (!(keyPtr = env->GetStringUTFChars(aKey, 0)))
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM"); LOGE(" ## serializeDataWithKeyJni(): failure - keyPtr JNI allocation OOM");
} }
@ -807,7 +813,9 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job
LOGD(" ## serializeDataWithKeyJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); LOGD(" ## serializeDataWithKeyJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
LOGD(" ## serializeDataWithKeyJni(): key=%s",(char const *)keyPtr); LOGD(" ## serializeDataWithKeyJni(): key=%s",(char const *)keyPtr);
if(NULL == (pickledPtr = (void*)malloc((pickledLength+1)*sizeof(uint8_t)))) void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t));
if (!pickledPtr)
{ {
LOGE(" ## serializeDataWithKeyJni(): failure - pickledPtr buffer OOM"); LOGE(" ## serializeDataWithKeyJni(): failure - pickledPtr buffer OOM");
} }
@ -818,7 +826,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job
keyLength, keyLength,
(void*)pickledPtr, (void*)pickledPtr,
pickledLength); pickledLength);
if(result == olm_error()) if (result == olm_error())
{ {
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);
@ -835,18 +843,15 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job
pickledDataRetValue = env->NewStringUTF((const char*)pickledPtr); pickledDataRetValue = env->NewStringUTF((const char*)pickledPtr);
LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr)); LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
} }
free(pickledPtr);
} }
} }
// free alloc // free alloc
if(NULL != keyPtr) if (keyPtr)
{ {
env->ReleaseStringUTFChars(aKey, keyPtr); env->ReleaseStringUTFChars(aKey, keyPtr);
}
if(NULL != pickledPtr)
{
free(pickledPtr);
} }
return pickledDataRetValue; return pickledDataRetValue;
@ -862,23 +867,23 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j
LOGD("## initWithSerializedDataJni(): IN"); LOGD("## initWithSerializedDataJni(): IN");
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE(" ## initWithSerializedDataJni(): failure - session failure OOM"); LOGE(" ## initWithSerializedDataJni(): failure - session failure OOM");
} }
else if(0 == aKey) else if (!aKey)
{ {
LOGE(" ## initWithSerializedDataJni(): failure - invalid key"); LOGE(" ## initWithSerializedDataJni(): failure - invalid key");
} }
else if(0 == aSerializedData) else if (!aSerializedData)
{ {
LOGE(" ## initWithSerializedDataJni(): failure - serialized data"); LOGE(" ## initWithSerializedDataJni(): failure - serialized data");
} }
else if(NULL == (keyPtr = env->GetStringUTFChars(aKey, 0))) else if (!(keyPtr = env->GetStringUTFChars(aKey, 0)))
{ {
LOGE(" ## initWithSerializedDataJni(): failure - keyPtr JNI allocation OOM"); LOGE(" ## initWithSerializedDataJni(): failure - keyPtr JNI allocation OOM");
} }
else if(NULL == (pickledPtr = env->GetStringUTFChars(aSerializedData, 0))) else if (!(pickledPtr = env->GetStringUTFChars(aSerializedData, 0)))
{ {
LOGE(" ## initWithSerializedDataJni(): failure - pickledPtr JNI allocation OOM"); LOGE(" ## initWithSerializedDataJni(): failure - pickledPtr JNI allocation OOM");
} }
@ -895,7 +900,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j
keyLength, keyLength,
(void*)pickledPtr, (void*)pickledPtr,
pickledLength); pickledLength);
if(result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); const char *errorMsgPtr = olm_session_last_error(sessionPtr);
LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_account() Msg=%s",errorMsgPtr); LOGE(" ## initWithSerializedDataJni(): failure - olm_unpickle_account() Msg=%s",errorMsgPtr);
@ -909,12 +914,12 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, j
} }
// free alloc // free alloc
if(NULL != keyPtr) if (keyPtr)
{ {
env->ReleaseStringUTFChars(aKey, keyPtr); env->ReleaseStringUTFChars(aKey, keyPtr);
} }
if(NULL != pickledPtr) if (pickledPtr)
{ {
env->ReleaseStringUTFChars(aSerializedData, pickledPtr); env->ReleaseStringUTFChars(aSerializedData, pickledPtr);
} }

View file

@ -21,17 +21,17 @@ using namespace AndroidOlmSdk;
OlmUtility* initializeUtilityMemory() OlmUtility* initializeUtilityMemory()
{ {
OlmUtility* utilityPtr = NULL;
size_t utilitySize = olm_utility_size(); size_t utilitySize = olm_utility_size();
OlmUtility* utilityPtr = (OlmUtility*)malloc(utilitySize);
if(NULL != (utilityPtr=(OlmUtility*)malloc(utilitySize))) if (utilityPtr)
{ {
utilityPtr = olm_utility(utilityPtr); utilityPtr = olm_utility(utilityPtr);
LOGD("## initializeUtilityMemory(): success - OLM utility size=%lu",static_cast<long unsigned int>(utilitySize)); LOGD("## initializeUtilityMemory(): success - OLM utility size=%lu",static_cast<long unsigned int>(utilitySize));
} }
else else
{ {
LOGE("## initializeUtilityMemory(): failure - OOM"); LOGE("## initializeUtilityMemory(): failure - OOM");
} }
return utilityPtr; return utilityPtr;
@ -39,12 +39,12 @@ OlmUtility* initializeUtilityMemory()
JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz) JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz)
{ {
OlmUtility* utilityPtr = NULL; OlmUtility* utilityPtr = initializeUtilityMemory();
LOGD("## initUtilityJni(): IN"); LOGD("## initUtilityJni(): IN");
// init account memory allocation // init account memory allocation
if(NULL == (utilityPtr = initializeUtilityMemory())) if (!utilityPtr)
{ {
LOGE(" ## initUtilityJni(): failure - init OOM"); LOGE(" ## initUtilityJni(): failure - init OOM");
} }
@ -59,19 +59,19 @@ JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz)
JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz) JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz)
{ {
OlmUtility* utilityPtr = NULL; OlmUtility* utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz);
LOGD("## releaseUtilityJni(): IN"); LOGD("## releaseUtilityJni(): IN");
if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) if (!utilityPtr)
{ {
LOGE("## releaseUtilityJni(): failure - utility ptr=NULL"); LOGE("## releaseUtilityJni(): failure - utility ptr=NULL");
} }
else else
{ {
olm_clear_utility(utilityPtr); olm_clear_utility(utilityPtr);
free(utilityPtr); free(utilityPtr);
} }
} }
@ -95,23 +95,23 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, j
LOGD("## verifyEd25519SignatureJni(): IN"); LOGD("## verifyEd25519SignatureJni(): IN");
if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) if (!(utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz)))
{ {
LOGE(" ## verifyEd25519SignatureJni(): failure - invalid utility ptr=NULL"); LOGE(" ## verifyEd25519SignatureJni(): failure - invalid utility ptr=NULL");
} }
else if((0 == aSignature) || (0 == aKey) || (0 == aMessage)) else if (!aSignature || !aKey || !aMessage)
{ {
LOGE(" ## verifyEd25519SignatureJni(): failure - invalid input parameters "); LOGE(" ## verifyEd25519SignatureJni(): failure - invalid input parameters ");
} }
else if(0 == (signaturePtr = env->GetStringUTFChars(aSignature, 0))) else if (!(signaturePtr = env->GetStringUTFChars(aSignature, 0)))
{ {
LOGE(" ## verifyEd25519SignatureJni(): failure - signature JNI allocation OOM"); LOGE(" ## verifyEd25519SignatureJni(): failure - signature JNI allocation OOM");
} }
else if(0 == (keyPtr = env->GetStringUTFChars(aKey, 0))) else if (!(keyPtr = env->GetStringUTFChars(aKey, 0)))
{ {
LOGE(" ## verifyEd25519SignatureJni(): failure - key JNI allocation OOM"); LOGE(" ## verifyEd25519SignatureJni(): failure - key JNI allocation OOM");
} }
else if(0 == (messagePtr = env->GetStringUTFChars(aMessage, 0))) else if (!(messagePtr = env->GetStringUTFChars(aMessage, 0)))
{ {
LOGE(" ## verifyEd25519SignatureJni(): failure - message JNI allocation OOM"); LOGE(" ## verifyEd25519SignatureJni(): failure - message JNI allocation OOM");
} }
@ -130,7 +130,7 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, j
messageLength, messageLength,
(void*)signaturePtr, (void*)signaturePtr,
signatureLength); signatureLength);
if(result == olm_error()) { if (result == olm_error()) {
const char *errorMsgPtr = olm_utility_last_error(utilityPtr); const char *errorMsgPtr = olm_utility_last_error(utilityPtr);
errorMessageRetValue = env->NewStringUTF(errorMsgPtr); errorMessageRetValue = env->NewStringUTF(errorMsgPtr);
LOGE("## verifyEd25519SignatureJni(): failure - olm_ed25519_verify Msg=%s",errorMsgPtr); LOGE("## verifyEd25519SignatureJni(): failure - olm_ed25519_verify Msg=%s",errorMsgPtr);
@ -142,17 +142,17 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, j
} }
// free alloc // free alloc
if(NULL != signaturePtr) if (signaturePtr)
{ {
env->ReleaseStringUTFChars(aSignature, signaturePtr); env->ReleaseStringUTFChars(aSignature, signaturePtr);
} }
if(NULL != keyPtr) if (keyPtr)
{ {
env->ReleaseStringUTFChars(aKey, keyPtr); env->ReleaseStringUTFChars(aKey, keyPtr);
} }
if(NULL != messagePtr) if (messagePtr)
{ {
env->ReleaseStringUTFChars(aMessage, messagePtr); env->ReleaseStringUTFChars(aMessage, messagePtr);
} }
@ -171,19 +171,18 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst
jstring sha256RetValue = 0; jstring sha256RetValue = 0;
OlmUtility* utilityPtr = NULL; OlmUtility* utilityPtr = NULL;
const char* messagePtr = NULL; const char* messagePtr = NULL;
void *hashValuePtr = NULL;
LOGD("## sha256Jni(): IN"); LOGD("## sha256Jni(): IN");
if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) if (!(utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz)))
{ {
LOGE(" ## sha256Jni(): failure - invalid utility ptr=NULL"); LOGE(" ## sha256Jni(): failure - invalid utility ptr=NULL");
} }
else if(0 == aMessageToHash) else if(!aMessageToHash)
{ {
LOGE(" ## sha256Jni(): failure - invalid message parameters "); LOGE(" ## sha256Jni(): failure - invalid message parameters ");
} }
else if(0 == (messagePtr = env->GetStringUTFChars(aMessageToHash, 0))) else if(!(messagePtr = env->GetStringUTFChars(aMessageToHash, 0)))
{ {
LOGE(" ## sha256Jni(): failure - message JNI allocation OOM"); LOGE(" ## sha256Jni(): failure - message JNI allocation OOM");
} }
@ -192,8 +191,9 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst
// get lengths // get lengths
size_t messageLength = (size_t)env->GetStringUTFLength(aMessageToHash); size_t messageLength = (size_t)env->GetStringUTFLength(aMessageToHash);
size_t hashLength = olm_sha256_length(utilityPtr); size_t hashLength = olm_sha256_length(utilityPtr);
void* hashValuePtr = malloc((hashLength+1)*sizeof(uint8_t));
if(NULL == (hashValuePtr = static_cast<void*>(malloc((hashLength+1)*sizeof(uint8_t))))) if (!hashValuePtr)
{ {
LOGE("## sha256Jni(): failure - hash value allocation OOM"); LOGE("## sha256Jni(): failure - hash value allocation OOM");
} }
@ -204,7 +204,7 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst
messageLength, messageLength,
(void *)hashValuePtr, (void *)hashValuePtr,
hashLength); hashLength);
if(result == olm_error()) if (result == olm_error())
{ {
LOGE("## sha256Jni(): failure - hash creation Msg=%s",(const char *)olm_utility_last_error(utilityPtr)); LOGE("## sha256Jni(): failure - hash creation Msg=%s",(const char *)olm_utility_last_error(utilityPtr));
} }
@ -216,16 +216,12 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jst
LOGD("## sha256Jni(): success - result=%lu hashValue=%s",static_cast<long unsigned int>(result), (char*)hashValuePtr); LOGD("## sha256Jni(): success - result=%lu hashValue=%s",static_cast<long unsigned int>(result), (char*)hashValuePtr);
sha256RetValue = env->NewStringUTF((const char*)hashValuePtr); sha256RetValue = env->NewStringUTF((const char*)hashValuePtr);
} }
free(hashValuePtr);
} }
} }
if(NULL != hashValuePtr) if (messagePtr)
{
free(hashValuePtr);
}
if(NULL != messagePtr)
{ {
env->ReleaseStringUTFChars(aMessageToHash, messagePtr); env->ReleaseStringUTFChars(aMessageToHash, messagePtr);
} }