diff options
Diffstat (limited to 'java/android/OlmLibSdk/olm-sdk/src/main/jni')
9 files changed, 329 insertions, 87 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp index 860ff3e..a10b8e0 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp @@ -16,6 +16,8 @@ #include "olm_account.h" +using namespace AndroidOlmSdk; + /** * Init memory allocation for account creation. * @return valid memory allocation, NULL otherwise @@ -264,36 +266,36 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t size_t keysResult; jbyteArray byteArrayRetValue = NULL; - LOGD("## oneTimeKeys(): accountPtr =%p",accountPtr); + LOGD("## oneTimeKeysJni(): IN"); if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) { - LOGE("## oneTimeKeys(): failure - invalid Account ptr"); + LOGE("## oneTimeKeysJni(): failure - invalid Account ptr"); } else { // keys memory allocation keysLength = olm_account_one_time_keys_length(accountPtr); if(NULL == (keysBytesPtr=(uint8_t *)malloc(keysLength*sizeof(uint8_t)))) { - LOGE("## oneTimeKeys(): failure - one time keys array OOM"); + LOGE("## oneTimeKeysJni(): failure - one time keys array OOM"); } else { // retrieve key pairs in keysBytesPtr keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); if(keysResult == olm_error()) { const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGE("## oneTimeKeys(): failure - error getting one time keys Msg=%s",errorMsgPtr); + LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",errorMsgPtr); } else { // allocate the byte array to be returned to java if(NULL == (byteArrayRetValue=env->NewByteArray(keysLength))) { - LOGE("## oneTimeKeys(): failure - return byte array OOM"); + LOGE("## oneTimeKeysJni(): failure - return byte array OOM"); } else { env->SetByteArrayRegion(byteArrayRetValue, 0/*offset*/, keysLength, (const jbyte*)keysBytesPtr); - LOGD("## oneTimeKeys(): success"); + LOGD("## oneTimeKeysJni(): success"); } } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp index 297f0f5..d10a05b 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp @@ -16,6 +16,7 @@ #include "olm_inbound_group_session.h" +using namespace AndroidOlmSdk; /** * Release the session allocation made by initializeInboundGroupSessionMemory().<br> diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h index e1fea02..70267ff 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h @@ -44,21 +44,16 @@ #define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name -// Error codes definition -static const int ERROR_CODE_OK = 0; -static const int ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS = ERROR_CODE_OK+1; -static const int ERROR_CODE_KO = -1; - -// constants -static const int ACCOUNT_CREATION_RANDOM_MODULO = 256; - - -// strings -static const char *CLASS_OLM_OUTBOUND_GROUP_SESSION = "org/matrix/olm/OlmOutboundGroupSession"; -static const char *CLASS_OLM_INBOUND_GROUP_SESSION = "org/matrix/olm/OlmInboundGroupSession"; -static const char *CLASS_OLM_SESSION = "org/matrix/olm/OlmSession"; -static const char *CLASS_OLM_ACCOUNT = "org/matrix/olm/OlmAccount"; -static const char *CLASS_OLM_UTILITY = "org/matrix/olm/OlmUtility"; +namespace AndroidOlmSdk +{ + // Error codes definition + static const int ERROR_CODE_OK = 0; + static const int ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS = ERROR_CODE_OK+1; + static const int ERROR_CODE_KO = -1; + + // constants + static const int ACCOUNT_CREATION_RANDOM_MODULO = 256; +} #ifdef __cplusplus extern "C" { @@ -70,7 +65,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); - +jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); #ifdef __cplusplus } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp index c518578..98baae1 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp @@ -17,7 +17,9 @@ * limitations under the License. */ -#include "olm_jni.h" +#include "olm_jni_helper.h" + +using namespace AndroidOlmSdk; /** * Init a buffer with a given number of random values. @@ -30,15 +32,15 @@ bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize) bool retCode = false; if(NULL == aBuffer2Ptr) { - LOGD("## setRandomInBuffer(): failure - aBuffer=NULL"); + LOGE("## setRandomInBuffer(): failure - aBuffer=NULL"); } else if(0 == aRandomSize) { - LOGD("## setRandomInBuffer(): failure - random size=0"); + LOGE("## setRandomInBuffer(): failure - random size=0"); } else if(NULL == (*aBuffer2Ptr = (uint8_t*)malloc(aRandomSize*sizeof(uint8_t)))) { - LOGD("## setRandomInBuffer(): failure - alloc mem OOM"); + LOGE("## setRandomInBuffer(): failure - alloc mem OOM"); } else { @@ -67,7 +69,7 @@ bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize) **/ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { - jlong instanceId=-1; + jlong instanceId = 0; jfieldID instanceIdField; jclass loaderClass; jclass requiredClass = 0; @@ -78,7 +80,7 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) { - LOGD("## getAccountInstanceId() failure - invalid instance of"); + LOGE("## getAccountInstanceId() failure - invalid instance of"); } else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { @@ -90,17 +92,17 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) } else { - LOGD("## getAccountInstanceId() ERROR! GetFieldID=null"); + LOGE("## getAccountInstanceId() ERROR! GetFieldID=null"); } } else { - LOGD("## getAccountInstanceId() ERROR! GetObjectClass=null"); + LOGE("## getAccountInstanceId() ERROR! GetObjectClass=null"); } } else { - LOGD("## getAccountInstanceId() ERROR! aJniEnv=NULL"); + LOGE("## getAccountInstanceId() ERROR! aJniEnv=NULL"); } LOGD("## getAccountInstanceId() success - instanceId=%lld",instanceId); return instanceId; @@ -114,7 +116,7 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) **/ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { - jlong instanceId=-1; + jlong instanceId = 0; jfieldID instanceIdField; jclass loaderClass; jclass requiredClass = 0; @@ -125,7 +127,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) { - LOGD("## getSessionInstanceId() failure - invalid instance of"); + LOGE("## getSessionInstanceId() failure - invalid instance of"); } else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { @@ -136,17 +138,17 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) } else { - LOGD("## getSessionInstanceId() ERROR! GetFieldID=null"); + LOGE("## getSessionInstanceId() ERROR! GetFieldID=null"); } } else { - LOGD("## getSessionInstanceId() ERROR! GetObjectClass=null"); + LOGE("## getSessionInstanceId() ERROR! GetObjectClass=null"); } } else { - LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL"); + LOGE("## getSessionInstanceId() ERROR! aJniEnv=NULL"); } //LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId); @@ -162,7 +164,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) **/ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { - jlong instanceId=-1; + jlong instanceId = 0; jfieldID instanceIdField; jclass loaderClass; jclass requiredClass = 0; @@ -173,7 +175,7 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) { - LOGD("## getInboundGroupSessionInstanceId() failure - invalid instance of"); + LOGE("## getInboundGroupSessionInstanceId() failure - invalid instance of"); } else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { @@ -184,17 +186,17 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) } else { - LOGD("## getInboundGroupSessionInstanceId() ERROR! GetFieldID=null"); + LOGE("## getInboundGroupSessionInstanceId() ERROR! GetFieldID=null"); } } else { - LOGD("## getInboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); + LOGE("## getInboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); } } else { - LOGD("## getInboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); + LOGE("## getInboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); } return instanceId; @@ -209,7 +211,7 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) **/ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { - jlong instanceId=-1; + jlong instanceId = 0; jfieldID instanceIdField; jclass loaderClass; jclass requiredClass = 0; @@ -220,7 +222,7 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) { - LOGD("## getOutboundGroupSessionInstanceId() failure - invalid instance of"); + LOGE("## getOutboundGroupSessionInstanceId() failure - invalid instance of"); } else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { @@ -231,17 +233,17 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) } else { - LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null"); + LOGE("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null"); } } else { - LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); + LOGE("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); } } else { - LOGD("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); + LOGE("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); } return instanceId; @@ -249,7 +251,7 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { - jlong instanceId=-1; + jlong instanceId = 0; jfieldID instanceIdField; jclass loaderClass; jclass requiredClass = 0; @@ -260,28 +262,28 @@ jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) { - LOGD("## getOutboundGroupSessionInstanceId() failure - invalid instance of"); + LOGE("## getUtilityInstanceId() failure - invalid instance of"); } else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { - if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmOutboundGroupSessionId", "J"))) + if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmUtilityId", "J"))) { instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); aJniEnv->DeleteLocalRef(loaderClass); } else { - LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null"); + LOGE("## getUtilityInstanceId() ERROR! GetFieldID=null"); } } else { - LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); + LOGE("## getUtilityInstanceId() ERROR! GetObjectClass=null"); } } else { - LOGD("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); + LOGE("## getUtilityInstanceId() ERROR! aJniEnv=NULL"); } return instanceId; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.h new file mode 100644 index 0000000..986a5a2 --- /dev/null +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.h @@ -0,0 +1,30 @@ +/** + * Created by pedrocon on 06/10/2016. + */ +/* + * Copyright 2016 OpenMarket Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "olm_jni.h" + +// constant strings +namespace AndroidOlmSdk +{ + static const char *CLASS_OLM_INBOUND_GROUP_SESSION = "org/matrix/olm/OlmInboundGroupSession"; + static const char *CLASS_OLM_OUTBOUND_GROUP_SESSION = "org/matrix/olm/OlmOutboundGroupSession"; + static const char *CLASS_OLM_SESSION = "org/matrix/olm/OlmSession"; + static const char *CLASS_OLM_ACCOUNT = "org/matrix/olm/OlmAccount"; + static const char *CLASS_OLM_UTILITY = "org/matrix/olm/OlmUtility"; +} diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp index 4cbd10b..55c6539 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp @@ -16,6 +16,7 @@ #include "olm_outbound_group_session.h" +using namespace AndroidOlmSdk; /** * Release the session allocation made by initializeOutboundGroupSessionMemory().<br> diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp index c02d390..188fe19 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp @@ -16,6 +16,7 @@ #include "olm_session.h" +using namespace AndroidOlmSdk; /** * Init memory allocation for a session creation.<br> @@ -585,31 +586,31 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { - LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL"); + LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL"); } else if(0 == aEncryptedMsg) { - LOGE("## decryptMessageJni(): failure - invalid encrypted message"); + LOGE("## decryptMessageJni(): failure - invalid encrypted message"); } else if(0 == (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;"))) { - 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"))) { - 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))) { - LOGE("## decryptMessageJni(): failure - JNI encrypted object "); + LOGE("## decryptMessageJni(): failure - JNI encrypted object "); } else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0))) { - LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); + LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); } else { @@ -621,7 +622,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t // create a dedicated temp buffer to be used in next Olm API calls tempEncryptedPtr = static_cast<char*>(malloc(encryptedMsgLength*sizeof(uint8_t))); memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - LOGD("## decryptMessageJni(): MsgType=%ld encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgType,encryptedMsgLength,encryptedMsgPtr); + LOGD("## decryptMessageJni(): MsgType=%ld encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgType,encryptedMsgLength,encryptedMsgPtr); // get max plaintext length size_t maxPlainTextLength = olm_decrypt_max_plaintext_length(sessionPtr, @@ -633,11 +634,11 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t if(maxPlainTextLength == olm_error()) { const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); + LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); } else { - LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",maxPlainTextLength); + LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",maxPlainTextLength); // allocate output decrypted message plainTextMsgPtr = static_cast<void*>(malloc((maxPlainTextLength+1)*sizeof(uint8_t))); @@ -653,14 +654,14 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t if(plaintextLength == olm_error()) { const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",errorMsgPtr); + LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",errorMsgPtr); } else { // update decrypted buffer size (static_cast<char*>(plainTextMsgPtr))[plaintextLength] = static_cast<char>('\0'); - LOGD("## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, static_cast<char*>(plainTextMsgPtr)); + LOGD("## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, static_cast<char*>(plainTextMsgPtr)); decryptedMsgRetValue = env->NewStringUTF(static_cast<const char*>(plainTextMsgPtr)); } } @@ -698,34 +699,41 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, job void *sessionIdPtr = NULL; jstring returnValueStr=0; - // get the size to alloc to contain the id - size_t lengthSessionId = olm_session_id_length(sessionPtr); + LOGD("## getSessionIdentifierJni(): IN "); if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL"); } - else if(NULL == (sessionIdPtr = (void*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) - { - LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM"); - } else { - size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId); - if (result == olm_error()) + // get the size to alloc to contain the id + size_t lengthSessionId = olm_session_id_length(sessionPtr); + LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",lengthSessionId); + + if(NULL == (sessionIdPtr = (void*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) { - const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr); + LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM"); } else { - // update length - (static_cast<char*>(sessionIdPtr))[result] = static_cast<char>('\0'); + size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId); - LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",result, (char*)sessionIdPtr); - returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); + if (result == olm_error()) + { + const char *errorMsgPtr = olm_session_last_error(sessionPtr); + LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr); + } + else + { + // update length + (static_cast<char*>(sessionIdPtr))[result] = static_cast<char>('\0'); + + LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",result, (char*)sessionIdPtr); + returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); + } + free(sessionIdPtr); } - free(sessionIdPtr); } return returnValueStr; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp index fb9c9c5..1bd82a0 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp @@ -14,24 +14,220 @@ * limitations under the License. */ -#include "olm_jni.h" #include "olm_utility.h" +using namespace AndroidOlmSdk; + +OlmUtility* initializeUtilityMemory() +{ + OlmUtility* utilityPtr = NULL; + size_t utilitySize = olm_utility_size(); + + if(NULL != (utilityPtr=(OlmUtility*)malloc(utilitySize))) + { + utilityPtr = olm_utility(utilityPtr); + LOGD("## initializeUtilityMemory(): success - OLM utility size=%lu",utilitySize); + } + else + { + LOGE("## initializeUtilityMemory(): failure - OOM"); + } + + return utilityPtr; +} + + +JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz) +{ + OlmUtility* utilityPtr = NULL; + + LOGD("## initUtilityJni(): IN"); + + // init account memory allocation + if(NULL == (utilityPtr = initializeUtilityMemory())) + { + LOGE(" ## initUtilityJni(): failure - init OOM"); + } + else + { + LOGD(" ## initUtilityJni(): success"); + } + + return (jlong)(intptr_t)utilityPtr; +} + + +JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz) +{ + OlmUtility* utilityPtr = NULL; + + if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) + { + LOGE("## releaseUtilityJni(): failure - utility ptr=NULL"); + } + else + { + olm_clear_utility(utilityPtr); + free(utilityPtr); + } +} + + /** * Verify an ed25519 signature. * If the key was too small then the message will be "OLM.INVALID_BASE64". * If the signature was invalid then the message will be "OLM.BAD_MESSAGE_MAC". - * @param aKey the ed25519 key. - * @param aMessage the message which was signed. - * @param aSignature the base64-encoded signature to be checked. + * + * @param aSignature the base64-encoded message signature to be checked. + * @param aKey the ed25519 key (fingerprint key) + * @param aMessage the message which was signed * @param the result error if there is a problem with the verification. * @return true if validation succeed, false otherwise */ -JNIEXPORT bool OLM_UTILITY_FUNC_DEF(ed25519VerifyJni)(JNIEnv *env, jobject thiz, jstring aKey, jstring aMessage, jstring aSignature, jobject aErrorMessage) +JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, jobject thiz, jstring aSignature, jstring aKey, jstring aMessage) +{ + jstring errorMessageRetValue = 0; + OlmUtility* utilityPtr = NULL; + const char* signaturePtr = NULL; + const char* keyPtr = NULL; + const char* messagePtr = NULL; + + LOGD("## verifyEd25519SignatureJni(): IN"); + + if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) + { + LOGE(" ## verifyEd25519SignatureJni(): failure - invalid utility ptr=NULL"); + } + else if((0 == aSignature) || (0 == aKey) || (0 == aMessage)) + { + LOGE(" ## verifyEd25519SignatureJni(): failure - invalid input parameters "); + } + else if(0 == (signaturePtr = env->GetStringUTFChars(aSignature, 0))) + { + LOGE(" ## verifyEd25519SignatureJni(): failure - signature JNI allocation OOM"); + } + else if(0 == (keyPtr = env->GetStringUTFChars(aKey, 0))) + { + LOGE(" ## verifyEd25519SignatureJni(): failure - key JNI allocation OOM"); + } + else if(0 == (messagePtr = env->GetStringUTFChars(aMessage, 0))) + { + LOGE(" ## verifyEd25519SignatureJni(): failure - message JNI allocation OOM"); + } + else + { + size_t signatureLength = (size_t)env->GetStringUTFLength(aSignature); + size_t keyLength = (size_t)env->GetStringUTFLength(aKey); + size_t messageLength = (size_t)env->GetStringUTFLength(aMessage); + LOGD(" ## verifyEd25519SignatureJni(): signatureLength=%lu keyLength=%lu messageLength=%lu",signatureLength,keyLength,messageLength); + + size_t result = olm_ed25519_verify(utilityPtr, + (void const *)keyPtr, + keyLength, + (void const *)messagePtr, + messageLength, + (void*)signaturePtr, + signatureLength); + if(result == olm_error()) { + const char *errorMsgPtr = olm_utility_last_error(utilityPtr); + errorMessageRetValue = env->NewStringUTF(errorMsgPtr); + LOGE("## verifyEd25519SignatureJni(): failure - session creation Msg=%s",errorMsgPtr); + } + else + { + LOGD("## verifyEd25519SignatureJni(): success - result=%ld", result); + } + } + + // free alloc + if(NULL != signaturePtr) + { + env->ReleaseStringUTFChars(aSignature, signaturePtr); + } + + if(NULL != keyPtr) + { + env->ReleaseStringUTFChars(aKey, keyPtr); + } + + if(NULL != messagePtr) + { + env->ReleaseStringUTFChars(aMessage, messagePtr); + } + + return errorMessageRetValue; +} + +/** +* Compute the digest (SHA 256) for the message passed in parameter.<br> +* The digest value is the function return value. +* @param aMessage +* @return digest of the message if operation succeed, null otherwise +**/ +JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jstring aMessageToHash) { - bool retCode = false; + jstring sha256RetValue = 0; + OlmUtility* utilityPtr = NULL; + const char* messagePtr = NULL; + void *hashValuePtr = NULL; + + LOGD("## sha256Jni(): IN"); + + if(NULL == (utilityPtr = (OlmUtility*)getUtilityInstanceId(env,thiz))) + { + LOGE(" ## sha256Jni(): failure - invalid utility ptr=NULL"); + } + else if(0 == aMessageToHash) + { + LOGE(" ## sha256Jni(): failure - invalid message parameters "); + } + else if(0 == (messagePtr = env->GetStringUTFChars(aMessageToHash, 0))) + { + LOGE(" ## sha256Jni(): failure - message JNI allocation OOM"); + } + else + { + // get lengths + size_t messageLength = (size_t)env->GetStringUTFLength(aMessageToHash); + size_t hashLength = olm_sha256_length(utilityPtr); + + if(NULL == (hashValuePtr = static_cast<void*>(malloc((hashLength+1)*sizeof(uint8_t))))) + { + LOGE("## sha256Jni(): failure - hash value allocation OOM"); + } + else + { + size_t result = olm_sha256(utilityPtr, + (void const *)messagePtr, + messageLength, + (void *)hashValuePtr, + hashLength); + if(result == olm_error()) + { + const char *errorMsgPtr = olm_utility_last_error(utilityPtr); + LOGE("## sha256Jni(): failure - hash creation Msg=%s",errorMsgPtr); + } + else + { + // update length + (static_cast<char*>(hashValuePtr))[result] = static_cast<char>('\0'); + + LOGD("## sha256Jni(): success - result=%lu hashValue=%s",result, (char*)hashValuePtr); + sha256RetValue = env->NewStringUTF((const char*)hashValuePtr); + } + } + + } + if(NULL != hashValuePtr) + { + free(hashValuePtr); + } + if(NULL != messagePtr) + { + env->ReleaseStringUTFChars(aMessageToHash, messagePtr); + } - return retCode; + return sha256RetValue; }
\ No newline at end of file diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h index 4e5772c..0bbbb52 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h @@ -17,13 +17,20 @@ #ifndef _OMLUTILITY_H #define _OMLUTILITY_H +#include "olm_jni.h" +#include "olm/olm.h" + #define OLM_UTILITY_FUNC_DEF(func_name) FUNC_DEF(OlmUtility,func_name) + #ifdef __cplusplus extern "C" { #endif -JNIEXPORT bool OLM_UTILITY_FUNC_DEF(ed25519VerifyJni)(JNIEnv *env, jobject thiz, jstring aKey, jstring aMessage, jstring aSignature, jobject aErrorMessage); +JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz); +JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz); +JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, jobject thiz, jstring aSignature, jstring aKey, jstring aMessage); +JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jstring aMessageToHash); #ifdef __cplusplus } |