Commit 99ff3d15 authored by ylecollen's avatar ylecollen
Browse files

remove the useless malloc(len+1) + zero terminated

parent 30c8d069
...@@ -262,7 +262,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { ...@@ -262,7 +262,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
OlmMessage encryptedMsgRetValue = new OlmMessage(); OlmMessage encryptedMsgRetValue = new OlmMessage();
try { try {
encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue); encryptedMsgRetValue.mCipherText = new String(encryptMessageJni(aClearMsg.getBytes("UTF-8"), encryptedMsgRetValue), "UTF-8");
} catch (Exception e) { } catch (Exception e) {
Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage()); Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage());
throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage()); throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage());
...@@ -271,7 +271,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { ...@@ -271,7 +271,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
return encryptedMsgRetValue; return encryptedMsgRetValue;
} }
private native void encryptMessageJni(byte[] aClearMsg, OlmMessage aEncryptedMsg); private native byte[] encryptMessageJni(byte[] aClearMsg, OlmMessage aEncryptedMsg);
/** /**
* Decrypt a message using the session.<br> * Decrypt a message using the session.<br>
......
...@@ -462,8 +462,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t ...@@ -462,8 +462,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t
// signature memory allocation // signature memory allocation
size_t signatureLength = olm_account_signature_length(accountPtr); size_t signatureLength = olm_account_signature_length(accountPtr);
size_t bufferLen = signatureLength + 1; void* signedMsgPtr = malloc(signatureLength * sizeof(uint8_t));
void* signedMsgPtr = malloc(bufferLen * sizeof(uint8_t));
if (!signedMsgPtr) if (!signedMsgPtr)
{ {
...@@ -486,14 +485,10 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t ...@@ -486,14 +485,10 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t
} }
else else
{ {
// info: signatureLength is always equal to resultSign
(static_cast<char*>(signedMsgPtr))[signatureLength] = static_cast<char>('\0');
LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength)); LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength));
signedMsgRetValueBuffer = env->NewByteArray(signatureLength); signedMsgRetValueBuffer = env->NewByteArray(signatureLength);
env->SetByteArrayRegion(signedMsgRetValueBuffer, 0 , signatureLength, (jbyte*)signedMsgPtr); env->SetByteArrayRegion(signedMsgRetValueBuffer, 0 , signatureLength, (jbyte*)signedMsgPtr);
} }
free(signedMsgPtr); free(signedMsgPtr);
...@@ -549,7 +544,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi ...@@ -549,7 +544,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); void *pickledPtr = malloc(pickledLength*sizeof(uint8_t));
if (!pickledPtr) if (!pickledPtr)
{ {
...@@ -570,11 +565,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi ...@@ -570,11 +565,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi
} }
else else
{ {
// build success output LOGD(" ## serializeJni(): success - result=%lu pickled starts with = %10s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
(static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
pickledDataRetValue = env->NewByteArray(pickledLength); pickledDataRetValue = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(pickledDataRetValue, 0 , pickledLength, (jbyte*)pickledPtr); env->SetByteArrayRegion(pickledDataRetValue, 0 , pickledLength, (jbyte*)pickledPtr);
} }
......
...@@ -167,7 +167,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN ...@@ -167,7 +167,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN
size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr); size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr);
LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)); uint8_t *sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t));
if (!sessionIdPtr) if (!sessionIdPtr)
{ {
...@@ -185,8 +185,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN ...@@ -185,8 +185,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JN
} }
else else
{ {
sessionIdPtr[result] = static_cast<char>('\0'); LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId starts with =%10s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
returnValue = env->NewByteArray(result); returnValue = env->NewByteArray(result);
env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr);
...@@ -280,7 +279,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn ...@@ -280,7 +279,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEn
uint32_t messageIndex = 0; uint32_t messageIndex = 0;
// allocate output decrypted message // allocate output decrypted message
uint8_t *plainTextMsgPtr = static_cast<uint8_t*>(malloc((maxPlainTextLength+1)*sizeof(uint8_t))); uint8_t *plainTextMsgPtr = static_cast<uint8_t*>(malloc(maxPlainTextLength*sizeof(uint8_t)));
// decrypt, but before reload encrypted buffer (previous one was destroyed) // decrypt, but before reload encrypted buffer (previous one was destroyed)
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
...@@ -370,7 +369,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en ...@@ -370,7 +369,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu", static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu", static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); void *pickledPtr = malloc(pickledLength*sizeof(uint8_t));
if (!pickledPtr) if (!pickledPtr)
{ {
...@@ -391,8 +390,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en ...@@ -391,8 +390,7 @@ JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *en
} }
else else
{ {
(static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0'); LOGD(" ## serializeJni(): success - result=%lu pickled starts with %10s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
pickledDataRet = env->NewByteArray(pickledLength); pickledDataRet = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(pickledDataRet, 0 , pickledLength, (jbyte*)pickledPtr); env->SetByteArrayRegion(pickledDataRet, 0 , pickledLength, (jbyte*)pickledPtr);
......
...@@ -161,7 +161,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(J ...@@ -161,7 +161,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(J
size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr); size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr);
LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
uint8_t *sessionIdPtr = (uint8_t*)malloc((lengthSessionId+1)*sizeof(uint8_t)); uint8_t *sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t));
if (!sessionIdPtr) if (!sessionIdPtr)
{ {
...@@ -179,13 +179,10 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(J ...@@ -179,13 +179,10 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(J
} }
else else
{ {
// update length
sessionIdPtr[result] = static_cast<char>('\0');
returnValue = env->NewByteArray(result); returnValue = env->NewByteArray(result);
env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr);
LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(sessionIdPtr)); LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId starts with %10s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(sessionIdPtr));
} }
// free alloc // free alloc
...@@ -251,7 +248,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv * ...@@ -251,7 +248,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *
size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr); size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr);
LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength)); LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength));
uint8_t *sessionKeyPtr = (uint8_t*)malloc((sessionKeyLength+1)*sizeof(uint8_t)); uint8_t *sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t));
if (!sessionKeyPtr) if (!sessionKeyPtr)
{ {
...@@ -269,9 +266,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv * ...@@ -269,9 +266,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *
} }
else else
{ {
// update length LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey starts with %10s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(sessionKeyPtr));
sessionKeyPtr[result] = static_cast<char>('\0');
LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%s",static_cast<long unsigned int>(result), reinterpret_cast<char*>(sessionKeyPtr));
returnValue = env->NewByteArray(result); returnValue = env->NewByteArray(result);
env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionKeyPtr); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionKeyPtr);
...@@ -323,7 +318,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE ...@@ -323,7 +318,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
// compute max encrypted length // compute max encrypted length
size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength); size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength);
uint8_t *encryptedMsgPtr = (uint8_t*)malloc((encryptedMsgLength+1)*sizeof(uint8_t)); uint8_t *encryptedMsgPtr = (uint8_t*)malloc(encryptedMsgLength*sizeof(uint8_t));
if (!encryptedMsgPtr) if (!encryptedMsgPtr)
{ {
...@@ -348,10 +343,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE ...@@ -348,10 +343,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
} }
else else
{ {
// update decrypted buffer size LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%10s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr));
encryptedMsgPtr[encryptedLength] = static_cast<char>('\0');
LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr));
encryptedMsgRet = env->NewByteArray(encryptedLength); encryptedMsgRet = env->NewByteArray(encryptedLength);
env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr); env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr);
...@@ -412,7 +404,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *e ...@@ -412,7 +404,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *e
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); void *pickledPtr = malloc(pickledLength*sizeof(uint8_t));
if(!pickledPtr) if(!pickledPtr)
{ {
...@@ -433,9 +425,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *e ...@@ -433,9 +425,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *e
} }
else else
{ {
// build success output LOGD(" ## serializeJni(): success - result=%lu pickled starts with %10s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
(static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
returnValue = env->NewByteArray(pickledLength); returnValue = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr); env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr);
......
...@@ -461,15 +461,16 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J ...@@ -461,15 +461,16 @@ JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(J
* Encrypt a message using the session.<br> * Encrypt a message using the session.<br>
* @param aClearMsg clear text message * @param aClearMsg clear text message
* @param [out] aEncryptedMsg ciphered message * @param [out] aEncryptedMsg ciphered message
* @return the encrypted message
*/ */
JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aEncryptedMsg) JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aEncryptedMsg)
{ {
jbyteArray encryptedMsgRet = 0;
const char* errorMessage = NULL; const char* errorMessage = NULL;
OlmSession *sessionPtr = getSessionInstanceId(env, thiz); OlmSession *sessionPtr = getSessionInstanceId(env, thiz);
jbyte *clearMsgPtr = NULL; jbyte *clearMsgPtr = NULL;
jclass encryptedMsgJClass = 0; jclass encryptedMsgJClass = 0;
jfieldID encryptedMsgFieldId;
jfieldID typeMsgFieldId; jfieldID typeMsgFieldId;
LOGD("## encryptMessageJni(): IN "); LOGD("## encryptMessageJni(): IN ");
...@@ -498,11 +499,6 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz ...@@ -498,11 +499,6 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
LOGE("## encryptMessageJni(): failure - unable to get crypted message class"); LOGE("## encryptMessageJni(): failure - unable to get crypted message class");
errorMessage = "unable to get crypted message class"; errorMessage = "unable to get crypted message class";
} }
else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;")))
{
LOGE("## encryptMessageJni(): failure - unable to get message field");
errorMessage = "unable to get message field";
}
else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J")))
{ {
LOGE("## encryptMessageJni(): failure - unable to get message type field"); LOGE("## encryptMessageJni(): failure - unable to get message type field");
...@@ -532,7 +528,7 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz ...@@ -532,7 +528,7 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer); size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer);
size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength); size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength);
void *encryptedMsgPtr = malloc((encryptedMsgLength+1)*sizeof(uint8_t)); void *encryptedMsgPtr = malloc(encryptedMsgLength*sizeof(uint8_t));
if (!encryptedMsgPtr) if (!encryptedMsgPtr)
{ {
...@@ -562,17 +558,13 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz ...@@ -562,17 +558,13 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
} }
else else
{ {
// update encrypted buffer size
(static_cast<char*>(encryptedMsgPtr))[result] = static_cast<char>('\0');
// update message type: PRE KEY or normal // update message type: PRE KEY or normal
env->SetLongField(aEncryptedMsg, typeMsgFieldId, (jlong)messageType); env->SetLongField(aEncryptedMsg, typeMsgFieldId, (jlong)messageType);
// update message: encryptedMsgPtr => encryptedJstring encryptedMsgRet = env->NewByteArray(encryptedMsgLength);
jstring encryptedJstring = env->NewStringUTF((const char*)encryptedMsgPtr); env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedMsgLength, (jbyte*)encryptedMsgPtr);
env->SetObjectField(aEncryptedMsg, encryptedMsgFieldId, (jobject)encryptedJstring);
LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg=%s", static_cast<long unsigned int>(result), static_cast<long unsigned int>(messageType), static_cast<long unsigned int>((size_t)env->GetStringUTFLength(encryptedJstring)), (const char*)encryptedMsgPtr); LOGD("## encryptMessageJni(): success - result=%lu Type=%lu utfLength=%lu encryptedMsg starts with %10s", static_cast<long unsigned int>(result), static_cast<long unsigned int>(messageType), static_cast<long unsigned int>((size_t)env->GetStringUTFLength(encryptedJstring)), (const char*)encryptedMsgPtr);
} }
free(encryptedMsgPtr); free(encryptedMsgPtr);
...@@ -592,6 +584,8 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz ...@@ -592,6 +584,8 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
{ {
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
} }
return encryptedMsgRet;
} }
/** /**
...@@ -682,7 +676,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobjec ...@@ -682,7 +676,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobjec
LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",static_cast<long unsigned int>(maxPlainTextLength)); LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",static_cast<long unsigned int>(maxPlainTextLength));
// allocate output decrypted message // allocate output decrypted message
plainTextMsgPtr = static_cast<uint8_t*>(malloc((maxPlainTextLength+1)*sizeof(uint8_t))); plainTextMsgPtr = static_cast<uint8_t*>(malloc(maxPlainTextLength*sizeof(uint8_t)));
// decrypt, but before reload encrypted buffer (previous one was destroyed) // decrypt, but before reload encrypted buffer (previous one was destroyed)
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
...@@ -756,7 +750,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, ...@@ -756,7 +750,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env,
size_t lengthSessionId = olm_session_id_length(sessionPtr); size_t lengthSessionId = olm_session_id_length(sessionPtr);
LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
void *sessionIdPtr = malloc((lengthSessionId+1)*sizeof(uint8_t)); void *sessionIdPtr = malloc(lengthSessionId*sizeof(uint8_t));
if (!sessionIdPtr) if (!sessionIdPtr)
{ {
...@@ -774,10 +768,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, ...@@ -774,10 +768,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env,
} }
else else
{ {
// update length LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId= starts with %10s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
(static_cast<char*>(sessionIdPtr))[result] = static_cast<char>('\0');
LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr);
returnValue = env->NewByteArray(result); returnValue = env->NewByteArray(result);
env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr);
...@@ -832,7 +823,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi ...@@ -832,7 +823,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
void *pickledPtr = malloc((pickledLength+1)*sizeof(uint8_t)); void *pickledPtr = malloc(pickledLength*sizeof(uint8_t));
if (!pickledPtr) if (!pickledPtr)
{ {
...@@ -853,9 +844,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi ...@@ -853,9 +844,7 @@ JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi
} }
else else
{ {
// build success output LOGD(" ## serializeJni(): success - result=%lu pickled starts with %s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
(static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0');
LOGD(" ## serializeJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr));
returnValue = env->NewByteArray(pickledLength); returnValue = env->NewByteArray(pickledLength);
env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr); env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr);
......
...@@ -43,7 +43,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje ...@@ -43,7 +43,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobje
JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg); JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg);
// encrypt/decrypt // encrypt/decrypt
JNIEXPORT void OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg); JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg);
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg); JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg);
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz); JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz);
......
Supports Markdown
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