signMessage : the utf8 conversion is done on Java side.

This commit is contained in:
ylecollen 2016-12-21 18:37:34 +01:00
parent e17eb69048
commit c3eb050be2
3 changed files with 46 additions and 37 deletions

View file

@ -383,9 +383,25 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* @return the signed message if operation succeed, null otherwise * @return the signed message if operation succeed, null otherwise
*/ */
public String signMessage(String aMessage) { public String signMessage(String aMessage) {
return signMessageJni(aMessage); if (null == aMessage) {
return null;
}
byte[] utf8String = null;
try {
utf8String = aMessage.getBytes("UTF-8");
} catch (Exception e) {
Log.d(LOG_TAG,"## signMessage(): failed ="+e.getMessage());
}
if (null == utf8String) {
return null;
}
return signMessageJni(utf8String);
} }
private native String signMessageJni(String aMessage); private native String signMessageJni(byte[] aMessage);
/** /**
* Return the number of unreleased OlmAccount instances.<br> * Return the number of unreleased OlmAccount instances.<br>

View file

@ -393,7 +393,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env,
* @param aMessage message to sign * @param aMessage message to sign
* @return the signed message, null otherwise * @return the signed message, null otherwise
**/ **/
JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jstring 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;
@ -411,48 +411,41 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz
} }
else else
{ {
// convert message from JAVA to C string int messageLength = env->GetArrayLength(aMessage);
const char* messageToSign = env->GetStringUTFChars(aMessage, 0); unsigned char* messageToSign = new unsigned char[messageLength];
if(NULL == messageToSign) env->GetByteArrayRegion(aMessage, 0, messageLength, reinterpret_cast<jbyte*>(messageToSign));
// signature memory allocation
signatureLength = olm_account_signature_length(accountPtr);
if(NULL == (signedMsgPtr = (void*)malloc((signatureLength+1)*sizeof(uint8_t))))
{ {
LOGE("## signMessageJni(): failure - message JNI allocation OOM"); LOGE("## signMessageJni(): failure - signature allocation OOM");
} }
else else
{ { // sign message
int messageLength = env->GetStringUTFLength(aMessage); resultSign = olm_account_sign(accountPtr,
(void*)messageToSign,
// signature memory allocation (size_t)messageLength,
signatureLength = olm_account_signature_length(accountPtr); signedMsgPtr,
if(NULL == (signedMsgPtr = (void*)malloc((signatureLength+1)*sizeof(uint8_t)))) signatureLength);
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));
}
else
{
// info: signatureLength is always equal to resultSign
(static_cast<char*>(signedMsgPtr))[signatureLength] = static_cast<char>('\0');
// convert to jstring
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(signedMsgPtr);
env->ReleaseStringUTFChars(aMessage, messageToSign);
} }
// release messageToSign
free(messageToSign);
} }
return signedMsgRetValue; return signedMsgRetValue;

View file

@ -43,7 +43,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env,
JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz); JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz);
// signing // signing
JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jstring aMessage); JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage);
// serialization // serialization
JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg); JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg);