Commit b893b81c authored by ylecollen's avatar ylecollen
Browse files

Simplify signMessageJni

parent 60bcf865
...@@ -396,65 +396,66 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, ...@@ -396,65 +396,66 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env,
**/ **/
JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage) JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage)
{ {
OlmAccount* accountPtr = NULL; OlmAccount* accountPtr = NULL;
size_t signatureLength; size_t signatureLength;
void* signedMsgPtr; void* signedMsgPtr;
size_t resultSign; size_t resultSign;
jstring signedMsgRetValue = NULL; jstring signedMsgRetValue = NULL;
if(NULL == aMessage) if(NULL == aMessage)
{ {
LOGE("## signMessageJni(): failure - invalid aMessage param"); LOGE("## signMessageJni(): failure - invalid aMessage param");
} }
else if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) else if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz)))
{
LOGE("## signMessageJni(): failure - invalid account ptr");
}
else
{
int messageLength = env->GetArrayLength(aMessage);
jbyte* messageToSign = env->GetByteArrayElements(aMessage, NULL);
// signature memory allocation
signatureLength = olm_account_signature_length(accountPtr);
if(NULL == (signedMsgPtr = (void*)malloc((signatureLength+1)*sizeof(uint8_t))))
{ {
LOGE("## signMessageJni(): failure - invalid account ptr"); LOGE("## signMessageJni(): failure - signature allocation OOM");
} }
else else
{ {
int messageLength = env->GetArrayLength(aMessage); // sign message
unsigned char* messageToSign = new unsigned char[messageLength]; resultSign = olm_account_sign(accountPtr,
env->GetByteArrayRegion(aMessage, 0, messageLength, reinterpret_cast<jbyte*>(messageToSign)); (void*)messageToSign,
(size_t)messageLength,
// signature memory allocation signedMsgPtr,
signatureLength = olm_account_signature_length(accountPtr); signatureLength);
if(NULL == (signedMsgPtr = (void*)malloc((signatureLength+1)*sizeof(uint8_t)))) if(resultSign == olm_error())
{ {
LOGE("## signMessageJni(): failure - signature allocation OOM"); LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr));
} }
else else
{ // sign message {
resultSign = olm_account_sign(accountPtr, // info: signatureLength is always equal to resultSign
(void*)messageToSign, (static_cast<char*>(signedMsgPtr))[signatureLength] = static_cast<char>('\0');
(size_t)messageLength, // convert to jstring
signedMsgPtr, signedMsgRetValue = env->NewStringUTF((const char*)signedMsgPtr); // UTF8
signatureLength); LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength));
if(resultSign == olm_error()) }
{
LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr)); free(signedMsgPtr);
} }
else
{ // release messageToSign
// info: signatureLength is always equal to resultSign if (messageToSign)
(static_cast<char*>(signedMsgPtr))[signatureLength] = static_cast<char>('\0'); {
// convert to jstring env->ReleaseByteArrayElements(aMessage, messageToSign, JNI_ABORT);
signedMsgRetValue = env->NewStringUTF((const char*)signedMsgPtr); // UTF8
LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength));
}
free(signedMsgPtr);
}
// release messageToSign
free(messageToSign);
} }
}
return signedMsgRetValue; return signedMsgRetValue;
} }
/** /**
* Serialize and encrypt account instance into a base64 string.<br> * Serialize and encrypt account instance into a base64 string.<br>
* @param aKey key used to encrypt the serialized account data * @param aKey key used to encrypt the serialized account data
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment