- Add new API for OlmUtility

- Introducing namespace AndroidOlmSdk
- Fix logs (function names mixed up)
- Add new check based on the calling java object instance type (IsInstanceOf())
- Fix return value for getXXXInstanceId() in case of failure. Now 0 is returned.
This commit is contained in:
pedroGitt 2016-10-18 15:59:36 +02:00
parent 4ccc45ab0a
commit 034fa6be40
9 changed files with 328 additions and 86 deletions

View file

@ -16,6 +16,8 @@
#include "olm_account.h" #include "olm_account.h"
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
@ -264,36 +266,36 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t
size_t keysResult; size_t keysResult;
jbyteArray byteArrayRetValue = NULL; jbyteArray byteArrayRetValue = NULL;
LOGD("## oneTimeKeys(): accountPtr =%p",accountPtr); LOGD("## oneTimeKeysJni(): IN");
if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz)))
{ {
LOGE("## oneTimeKeys(): failure - invalid Account ptr"); LOGE("## oneTimeKeysJni(): failure - invalid Account ptr");
} }
else else
{ // keys memory allocation { // keys memory allocation
keysLength = olm_account_one_time_keys_length(accountPtr); keysLength = olm_account_one_time_keys_length(accountPtr);
if(NULL == (keysBytesPtr=(uint8_t *)malloc(keysLength*sizeof(uint8_t)))) 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 else
{ // retrieve key pairs in keysBytesPtr { // retrieve key pairs in keysBytesPtr
keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength);
if(keysResult == olm_error()) { if(keysResult == olm_error()) {
const char *errorMsgPtr = olm_account_last_error(accountPtr); 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 else
{ // allocate the byte array to be returned to java { // allocate the byte array to be returned to java
if(NULL == (byteArrayRetValue=env->NewByteArray(keysLength))) if(NULL == (byteArrayRetValue=env->NewByteArray(keysLength)))
{ {
LOGE("## oneTimeKeys(): failure - return byte array OOM"); LOGE("## oneTimeKeysJni(): failure - return byte array OOM");
} }
else else
{ {
env->SetByteArrayRegion(byteArrayRetValue, 0/*offset*/, keysLength, (const jbyte*)keysBytesPtr); env->SetByteArrayRegion(byteArrayRetValue, 0/*offset*/, keysLength, (const jbyte*)keysBytesPtr);
LOGD("## oneTimeKeys(): success"); LOGD("## oneTimeKeysJni(): success");
} }
} }

View file

@ -16,6 +16,7 @@
#include "olm_inbound_group_session.h" #include "olm_inbound_group_session.h"
using namespace AndroidOlmSdk;
/** /**
* Release the session allocation made by initializeInboundGroupSessionMemory().<br> * Release the session allocation made by initializeInboundGroupSessionMemory().<br>

View file

@ -44,6 +44,8 @@
#define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name #define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name
namespace AndroidOlmSdk
{
// Error codes definition // Error codes definition
static const int ERROR_CODE_OK = 0; 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_NO_MATCHING_ONE_TIME_KEYS = ERROR_CODE_OK+1;
@ -51,14 +53,7 @@ static const int ERROR_CODE_KO = -1;
// constants // constants
static const int ACCOUNT_CREATION_RANDOM_MODULO = 256; 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";
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -70,7 +65,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
#ifdef __cplusplus #ifdef __cplusplus
} }

View file

@ -17,7 +17,9 @@
* limitations under the License. * 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. * 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; bool retCode = false;
if(NULL == aBuffer2Ptr) if(NULL == aBuffer2Ptr)
{ {
LOGD("## setRandomInBuffer(): failure - aBuffer=NULL"); LOGE("## setRandomInBuffer(): failure - aBuffer=NULL");
} }
else if(0 == aRandomSize) 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)))) else if(NULL == (*aBuffer2Ptr = (uint8_t*)malloc(aRandomSize*sizeof(uint8_t))))
{ {
LOGD("## setRandomInBuffer(): failure - alloc mem OOM"); LOGE("## setRandomInBuffer(): failure - alloc mem OOM");
} }
else else
{ {
@ -67,7 +69,7 @@ bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize)
**/ **/
jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId=-1; jlong instanceId = 0;
jfieldID instanceIdField; jfieldID instanceIdField;
jclass loaderClass; jclass loaderClass;
jclass requiredClass = 0; jclass requiredClass = 0;
@ -78,7 +80,7 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) 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))) else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{ {
@ -90,17 +92,17 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
} }
else else
{ {
LOGD("## getAccountInstanceId() ERROR! GetFieldID=null"); LOGE("## getAccountInstanceId() ERROR! GetFieldID=null");
} }
} }
else else
{ {
LOGD("## getAccountInstanceId() ERROR! GetObjectClass=null"); LOGE("## getAccountInstanceId() ERROR! GetObjectClass=null");
} }
} }
else else
{ {
LOGD("## getAccountInstanceId() ERROR! aJniEnv=NULL"); LOGE("## getAccountInstanceId() ERROR! aJniEnv=NULL");
} }
LOGD("## getAccountInstanceId() success - instanceId=%lld",instanceId); LOGD("## getAccountInstanceId() success - instanceId=%lld",instanceId);
return instanceId; return instanceId;
@ -114,7 +116,7 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId=-1; jlong instanceId = 0;
jfieldID instanceIdField; jfieldID instanceIdField;
jclass loaderClass; jclass loaderClass;
jclass requiredClass = 0; jclass requiredClass = 0;
@ -125,7 +127,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) 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))) else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{ {
@ -136,17 +138,17 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
} }
else else
{ {
LOGD("## getSessionInstanceId() ERROR! GetFieldID=null"); LOGE("## getSessionInstanceId() ERROR! GetFieldID=null");
} }
} }
else else
{ {
LOGD("## getSessionInstanceId() ERROR! GetObjectClass=null"); LOGE("## getSessionInstanceId() ERROR! GetObjectClass=null");
} }
} }
else else
{ {
LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL"); LOGE("## getSessionInstanceId() ERROR! aJniEnv=NULL");
} }
//LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId); //LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId);
@ -162,7 +164,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId=-1; jlong instanceId = 0;
jfieldID instanceIdField; jfieldID instanceIdField;
jclass loaderClass; jclass loaderClass;
jclass requiredClass = 0; jclass requiredClass = 0;
@ -173,7 +175,7 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) 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))) else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{ {
@ -184,17 +186,17 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
} }
else else
{ {
LOGD("## getInboundGroupSessionInstanceId() ERROR! GetFieldID=null"); LOGE("## getInboundGroupSessionInstanceId() ERROR! GetFieldID=null");
} }
} }
else else
{ {
LOGD("## getInboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); LOGE("## getInboundGroupSessionInstanceId() ERROR! GetObjectClass=null");
} }
} }
else else
{ {
LOGD("## getInboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); LOGE("## getInboundGroupSessionInstanceId() ERROR! aJniEnv=NULL");
} }
return instanceId; return instanceId;
@ -209,7 +211,7 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId=-1; jlong instanceId = 0;
jfieldID instanceIdField; jfieldID instanceIdField;
jclass loaderClass; jclass loaderClass;
jclass requiredClass = 0; jclass requiredClass = 0;
@ -220,7 +222,7 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) 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))) else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{ {
@ -231,17 +233,17 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
} }
else else
{ {
LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null"); LOGE("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null");
} }
} }
else else
{ {
LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); LOGE("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null");
} }
} }
else else
{ {
LOGD("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); LOGE("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL");
} }
return instanceId; return instanceId;
@ -249,7 +251,7 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId=-1; jlong instanceId = 0;
jfieldID instanceIdField; jfieldID instanceIdField;
jclass loaderClass; jclass loaderClass;
jclass requiredClass = 0; jclass requiredClass = 0;
@ -260,28 +262,28 @@ jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) 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))) 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); instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
aJniEnv->DeleteLocalRef(loaderClass); aJniEnv->DeleteLocalRef(loaderClass);
} }
else else
{ {
LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null"); LOGE("## getUtilityInstanceId() ERROR! GetFieldID=null");
} }
} }
else else
{ {
LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); LOGE("## getUtilityInstanceId() ERROR! GetObjectClass=null");
} }
} }
else else
{ {
LOGD("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); LOGE("## getUtilityInstanceId() ERROR! aJniEnv=NULL");
} }
return instanceId; return instanceId;

View file

@ -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";
}

View file

@ -16,6 +16,7 @@
#include "olm_outbound_group_session.h" #include "olm_outbound_group_session.h"
using namespace AndroidOlmSdk;
/** /**
* Release the session allocation made by initializeOutboundGroupSessionMemory().<br> * Release the session allocation made by initializeOutboundGroupSessionMemory().<br>

View file

@ -16,6 +16,7 @@
#include "olm_session.h" #include "olm_session.h"
using namespace AndroidOlmSdk;
/** /**
* Init memory allocation for a session creation.<br> * Init memory allocation for a session creation.<br>
@ -698,20 +699,26 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, job
void *sessionIdPtr = NULL; void *sessionIdPtr = NULL;
jstring returnValueStr=0; jstring returnValueStr=0;
// get the size to alloc to contain the id LOGD("## getSessionIdentifierJni(): IN ");
size_t lengthSessionId = olm_session_id_length(sessionPtr);
if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz)))
{ {
LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL"); LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL");
} }
else if(NULL == (sessionIdPtr = (void*)malloc((lengthSessionId+1)*sizeof(uint8_t)))) else
{
// 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))))
{ {
LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM"); LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM");
} }
else else
{ {
size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId); size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_session_last_error(sessionPtr); const char *errorMsgPtr = olm_session_last_error(sessionPtr);
@ -727,6 +734,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, job
} }
free(sessionIdPtr); free(sessionIdPtr);
} }
}
return returnValueStr; return returnValueStr;
} }

View file

@ -14,24 +14,220 @@
* limitations under the License. * limitations under the License.
*/ */
#include "olm_jni.h"
#include "olm_utility.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. * Verify an ed25519 signature.
* If the key was too small then the message will be "OLM.INVALID_BASE64". * 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". * 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 message signature to be checked.
* @param aSignature the base64-encoded 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. * @param the result error if there is a problem with the verification.
* @return true if validation succeed, false otherwise * @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)
{ {
bool retCode = false; 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)))
return retCode; {
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)
{
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 sha256RetValue;
} }

View file

@ -17,13 +17,20 @@
#ifndef _OMLUTILITY_H #ifndef _OMLUTILITY_H
#define _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) #define OLM_UTILITY_FUNC_DEF(func_name) FUNC_DEF(OlmUtility,func_name)
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #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 #ifdef __cplusplus
} }