encryptMessage : the UTF8 conversion is done on JAVA side.

This commit is contained in:
ylecollen 2017-01-03 11:38:43 +01:00
parent e7c7d77a8a
commit de962ef8d7
3 changed files with 13 additions and 9 deletions

View file

@ -235,12 +235,16 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
String retValue = null; String retValue = null;
if(!TextUtils.isEmpty(aClearMsg)) { if(!TextUtils.isEmpty(aClearMsg)) {
retValue = encryptMessageJni(aClearMsg); try {
retValue = encryptMessageJni(aClearMsg.getBytes("UTF-8"));
} catch (Exception e) {
Log.e(LOG_TAG, "## encryptMessage() failed " + e.getMessage());
}
} }
return retValue; return retValue;
} }
private native String encryptMessageJni(String aClearMsg); private native String encryptMessageJni(byte[] aClearMsgBuffer);
/** /**
* Return true the object resources have been released.<br> * Return true the object resources have been released.<br>

View file

@ -262,30 +262,30 @@ 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, jbyteArray aClearMsgBuffer)
{ {
LOGD("## encryptMessageJni(): IN"); LOGD("## encryptMessageJni(): IN");
jstring encryptedMsgRetValue = 0; jstring encryptedMsgRetValue = 0;
OlmOutboundGroupSession *sessionPtr = NULL; OlmOutboundGroupSession *sessionPtr = NULL;
const char *clearMsgPtr = NULL; jbyte* clearMsgPtr = NULL;
if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if (!(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 (!aClearMsg) else if (!aClearMsgBuffer)
{ {
LOGE(" ## encryptMessageJni(): failure - invalid clear message"); LOGE(" ## encryptMessageJni(): failure - invalid clear message");
} }
else if (!(clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) else if (!(clearMsgPtr = env->GetByteArrayElements(aClearMsgBuffer, NULL)))
{ {
LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM"); LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM");
} }
else else
{ {
// get clear message length // get clear message length
size_t clearMsgLength = (size_t)env->GetStringUTFLength(aClearMsg); size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer);
LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",static_cast<long unsigned int>(clearMsgLength)); LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",static_cast<long unsigned int>(clearMsgLength));
// compute max encrypted length // compute max encrypted length
@ -325,7 +325,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv
// free alloc // free alloc
if (clearMsgPtr) if (clearMsgPtr)
{ {
env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr); env->ReleaseByteArrayElements(aClearMsgBuffer, clearMsgPtr, JNI_ABORT);
} }
return encryptedMsgRetValue; return encryptedMsgRetValue;

View file

@ -37,7 +37,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE
JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz); JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz);
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);
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsgPtr); JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer);
// serialization // serialization
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg); JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg);