diff options
author | ylecollen <ylecollen@amdocs.com> | 2017-01-03 16:12:20 +0100 |
---|---|---|
committer | ylecollen <ylecollen@amdocs.com> | 2017-01-03 16:12:20 +0100 |
commit | 9552e14fdadd9fadbe88970cfb8df7b697c0deb4 (patch) | |
tree | fd955c9b67c06eae88a4e3e8d796e7b2017858da /java | |
parent | 765647cda501b53bac990332444aadc4404fe314 (diff) |
-> the byte[] to String conversions are done on Java level (when it is possible)
-> remove javaCStringToUtf8
Diffstat (limited to 'java')
17 files changed, 174 insertions, 155 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java index 5c1ad3c..1e41fa1 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java @@ -118,7 +118,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { } else { aErrorMsg.setLength(0); try { - pickleRetValue = serializeDataWithKeyJni(aKey.getBytes("UTF-8"), aErrorMsg); + pickleRetValue = new String(serializeDataWithKeyJni(aKey.getBytes("UTF-8"), aErrorMsg), "UTF-8"); } catch (Exception e) { Log.e(LOG_TAG, "## serializeDataWithKey() failed " + e.getMessage()); aErrorMsg.append(e.getMessage()); @@ -127,7 +127,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { return pickleRetValue; } - private native String serializeDataWithKeyJni(byte[] aKey, StringBuffer aErrorMsg); + private native byte[] serializeDataWithKeyJni(byte[] aKey, StringBuffer aErrorMsg); /** @@ -164,6 +164,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { return retCode; } + private native String initWithSerializedDataJni(byte[] aSerializedDataBuffer, byte[] aKeyBuffer); /** @@ -363,25 +364,34 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * @return the signed message if operation succeed, null otherwise */ public String signMessage(String aMessage) { - if (null == aMessage) { - return null; - } + String result = null; - byte[] utf8String = null; + if (null != aMessage) { + byte[] utf8String = null; - try { - utf8String = aMessage.getBytes("UTF-8"); - } catch (Exception e) { - Log.d(LOG_TAG,"## signMessage(): failed ="+e.getMessage()); - } + try { + utf8String = aMessage.getBytes("UTF-8"); + } catch (Exception e) { + Log.e(LOG_TAG, "## signMessage(): failed =" + e.getMessage()); + } - if (null == utf8String) { - return null; + if (null != utf8String) { + byte[] signedMessage = signMessageJni(utf8String); + + if (null != signedMessage) { + try { + result = new String(signedMessage, "UTF-8"); + } catch (Exception e) { + Log.e(LOG_TAG, "## signMessage(): failed =" + e.getMessage()); + } + } + } } - return signMessageJni(utf8String); + return result; } - private native String signMessageJni(byte[] aMessage); + + private native byte[] signMessageJni(byte[] aMessage); /** * Return true the object resources have been released.<br> diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java index 7ec5b2f..bfd50fb 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java @@ -144,10 +144,16 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * @return the session ID if operation succeed, null otherwise */ public String sessionIdentifier() { - return sessionIdentifierJni(); + try { + return new String(sessionIdentifierJni(), "UTF-8"); + } catch (Exception e) { + Log.e(LOG_TAG, "## sessionIdentifier() failed " + e.getMessage()); + } + + return null; } - private native String sessionIdentifierJni(); + private native byte[] sessionIdentifierJni(); /** * Decrypt the message passed in parameter.<br> @@ -161,9 +167,14 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri StringBuffer errorMsg = new StringBuffer(); try { - result.mDecryptedMessage = decryptMessageJni(aEncryptedMsg.getBytes("UTF-8"), result, errorMsg); + byte[] decryptedMessageBuffer = decryptMessageJni(aEncryptedMsg.getBytes("UTF-8"), result, errorMsg); + + if (null != decryptedMessageBuffer) { + result.mDecryptedMessage = new String(decryptedMessageBuffer, "UTF-8"); + } } catch (Exception e) { Log.e(LOG_TAG, "## decryptMessage() failed " + e.getMessage()); + errorMsg.append(e.getMessage()); } // check if there is an error while decrypting @@ -174,7 +185,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri return result; } - private native String decryptMessageJni(byte[] aEncryptedMsg, DecryptMessageResult aDecryptMessageResult, StringBuffer aErrorMsg); + private native byte[] decryptMessageJni(byte[] aEncryptedMsg, DecryptMessageResult aDecryptMessageResult, StringBuffer aErrorMsg); /** * Kick off the serialization mechanism. @@ -226,9 +237,10 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri } else { aErrorMsg.setLength(0); try { - pickleRetValue = serializeDataWithKeyJni(aKey.getBytes("UTF-8"), aErrorMsg); + pickleRetValue = new String(serializeDataWithKeyJni(aKey.getBytes("UTF-8"), aErrorMsg), "UTF-8"); } catch (Exception e) { Log.e(LOG_TAG, "## serializeDataWithKey() failed " + e.getMessage()); + aErrorMsg.append(e.getMessage()); } } @@ -240,7 +252,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * @param aErrorMsg error message description * @return pickled base64 string if operation succeed, null otherwise */ - private native String serializeDataWithKeyJni(byte[] aKey, StringBuffer aErrorMsg); + private native byte[] serializeDataWithKeyJni(byte[] aKey, StringBuffer aErrorMsg); /** diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java index bfb2cd1..171891c 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java @@ -208,10 +208,16 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser * @return session identifier if operation succeed, null otherwise. */ public String sessionIdentifier() { - return sessionIdentifierJni(); + try { + return new String(sessionIdentifierJni(), "UTF-8"); + } catch (Exception e) { + Log.e(LOG_TAG, "## sessionIdentifier() failed " + e.getMessage()); + } + + return null; } - private native String sessionIdentifierJni(); + private native byte[] sessionIdentifierJni(); /** * Get the current message index for this session.<br> @@ -231,9 +237,16 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser * @return outbound session key */ public String sessionKey() { - return sessionKeyJni(); + try { + return new String(sessionKeyJni(), "UTF-8"); + } catch (Exception e) { + Log.e(LOG_TAG, "## sessionKey() failed " + e.getMessage()); + } + + return null; } - private native String sessionKeyJni(); + + private native byte[] sessionKeyJni(); /** * Encrypt some plain-text message.<br> @@ -246,7 +259,7 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser if(!TextUtils.isEmpty(aClearMsg)) { try { - retValue = encryptMessageJni(aClearMsg.getBytes("UTF-8")); + retValue = new String(encryptMessageJni(aClearMsg.getBytes("UTF-8")), "UTF-8"); } catch (Exception e) { Log.e(LOG_TAG, "## encryptMessage() failed " + e.getMessage()); } @@ -254,7 +267,7 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser return retValue; } - private native String encryptMessageJni(byte[] aClearMsgBuffer); + private native byte[] encryptMessageJni(byte[] aClearMsgBuffer); /** * Return true the object resources have been released.<br> diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java index 7b9b736..a58036f 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java @@ -302,10 +302,16 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { * @return the session ID as a String if operation succeed, null otherwise */ public String sessionIdentifier() { - return getSessionIdentifierJni(); + try { + return new String(getSessionIdentifierJni(), "UTF-8"); + } catch (Exception e) { + Log.e(LOG_TAG, "## sessionIdentifier(): " + e.getMessage()); + } + + return null; } - private native String getSessionIdentifierJni(); + private native byte[] getSessionIdentifierJni(); /** * Checks if the PRE_KEY({@link OlmMessage#MESSAGE_TYPE_PRE_KEY}) message is for this in-bound session.<br> @@ -383,10 +389,16 @@ public class OlmSession extends CommonSerializeUtils implements Serializable { * @return the decrypted message if operation succeed, null otherwise */ public String decryptMessage(OlmMessage aEncryptedMsg) { - return decryptMessageJni(aEncryptedMsg); + try { + return new String(decryptMessageJni(aEncryptedMsg), "UTF-8"); + } catch (Exception e) { + Log.e(LOG_TAG, "## decryptMessage(): failed " + e.getMessage()); + } + + return null; } - private native String decryptMessageJni(OlmMessage aEncryptedMsg); + private native byte[] decryptMessageJni(OlmMessage aEncryptedMsg); /** * Return true the object resources have been released.<br> diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java index 3d9d99d..287a9d2 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java @@ -121,7 +121,7 @@ public class OlmUtility { if (null != aMessageToHash) { try { - hashRetValue = sha256Jni(aMessageToHash.getBytes("UTF-8")); + hashRetValue = new String(sha256Jni(aMessageToHash.getBytes("UTF-8")), "UTF-8"); } catch (Exception e) { Log.e(LOG_TAG, "## sha256(): failed " + e.getMessage()); } @@ -130,7 +130,7 @@ public class OlmUtility { return hashRetValue; } - private native String sha256Jni(byte[] aMessage); + private native byte[] sha256Jni(byte[] aMessage); /** diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp index 87ed86a..06709e7 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp @@ -405,10 +405,10 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, * @param aMessage message to sign * @return the signed message, null otherwise **/ -JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage) +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage) { OlmAccount* accountPtr = NULL; - jstring signedMsgRetValue = NULL; + jbyteArray signedMsgRetValueBuffer = NULL; if (!aMessage) { @@ -425,7 +425,8 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz // signature memory allocation size_t signatureLength = olm_account_signature_length(accountPtr); - void* signedMsgPtr = malloc((signatureLength+1)*sizeof(uint8_t)); + size_t bufferLen = signatureLength + 1; + void* signedMsgPtr = malloc(bufferLen * sizeof(uint8_t)); if (!signedMsgPtr) { @@ -448,9 +449,12 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz { // 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)); + + signedMsgRetValueBuffer = env->NewByteArray(signatureLength); + env->SetByteArrayRegion(signedMsgRetValueBuffer, 0 , signatureLength, (jbyte*)signedMsgPtr); + } free(signedMsgPtr); @@ -463,7 +467,7 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz } } - return signedMsgRetValue; + return signedMsgRetValueBuffer; } /** @@ -472,9 +476,9 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz * @param[out] aErrorMsg error message set if operation failed * @return a base64 string if operation succeed, null otherwise **/ -JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg) +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg) { - jstring pickledDataRetValue = 0; + jbyteArray pickledDataRetValue = 0; jclass errorMsgJClass = 0; jmethodID errorMsgMethodId = 0; jstring errorJstring = 0; @@ -541,8 +545,11 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, job { // build success output (static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0'); - pickledDataRetValue = env->NewStringUTF((const char*)pickledPtr); + LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr)); + + pickledDataRetValue = env->NewByteArray(pickledLength+1); + env->SetByteArrayRegion(pickledDataRetValue, 0 , pickledLength+1, (jbyte*)pickledPtr); } free(pickledPtr); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h index 88ef215..c866dbc 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h @@ -44,10 +44,10 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env, JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz); // signing -JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage); +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage); // serialization -JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg); +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg); JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer); #ifdef __cplusplus diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp index 9eba3af..ca47dbb 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp @@ -137,10 +137,10 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes /** * Get a base64-encoded identifier for this inbound group session. */ -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) +JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) { OlmInboundGroupSession *sessionPtr = NULL; - jstring returnValueStr=0; + jbyteArray returnValue = 0; LOGD("## sessionIdentifierJni(): inbound group session IN"); @@ -170,22 +170,26 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn } else { - // update length + sessionIdPtr[result] = static_cast<char>('\0'); LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr); - returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); + + returnValue = env->NewByteArray(result); + env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); } + free(sessionIdPtr); } } - return returnValueStr; + return returnValue; } -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsgBuffer, jobject aDecryptionResult, jobject aErrorMsg) +JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsgBuffer, jobject aDecryptionResult, jobject aErrorMsg) { - jstring decryptedMsgRetValue = 0; + jbyteArray decryptedMsgBuffer = 0; + OlmInboundGroupSession *sessionPtr = NULL; jbyte *encryptedMsgPtr = NULL; jclass indexObjJClass = 0; @@ -298,17 +302,10 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * // update index env->SetLongField(aDecryptionResult, indexMsgFieldId, (jlong)messageIndex); - // convert to utf8 - decryptedMsgRetValue = javaCStringToUtf8(env, plainTextMsgPtr, plaintextLength); + decryptedMsgBuffer = env->NewByteArray(plaintextLength); + env->SetByteArrayRegion(decryptedMsgBuffer, 0 , plaintextLength, (jbyte*)plainTextMsgPtr); - if (!decryptedMsgRetValue) - { - LOGE(" ## decryptMessageJni(): UTF-8 Conversion failure - javaCStringToUtf8() returns null"); - } - else - { - LOGD(" ## decryptMessageJni(): UTF-8 Conversion - decrypted returnedLg=%lu OK",static_cast<long unsigned int>(plaintextLength)); - } + LOGD(" ## decryptMessageJni(): UTF-8 Conversion - decrypted returnedLg=%lu OK",static_cast<long unsigned int>(plaintextLength)); } if (plainTextMsgPtr) @@ -330,7 +327,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * env->ReleaseByteArrayElements(aEncryptedMsgBuffer, encryptedMsgPtr, JNI_ABORT); } - return decryptedMsgRetValue; + return decryptedMsgBuffer; } @@ -340,9 +337,10 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * * @param[out] aErrorMsg error message set if operation failed * @return a base64 string if operation succeed, null otherwise **/ -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg) +JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg) { - jstring pickledDataRetValue = 0; + jbyteArray pickledDataRet = 0; + jclass errorMsgJClass = 0; jmethodID errorMsgMethodId = 0; jbyte* keyPtr = NULL; @@ -408,10 +406,11 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JN } else { - // build success output - (static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0'); - pickledDataRetValue = env->NewStringUTF((const char*)pickledPtr); + (static_cast<char*>(pickledPtr))[pickledLength] = static_cast<char>('\0'); LOGD(" ## serializeDataWithKeyJni(): success - result=%lu pickled=%s", static_cast<long unsigned int>(result), static_cast<char*>(pickledPtr)); + + pickledDataRet = env->NewByteArray(pickledLength); + env->SetByteArrayRegion(pickledDataRet, 0 , pickledLength, (jbyte*)pickledPtr); } free(pickledPtr); @@ -424,7 +423,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JN env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); } - return pickledDataRetValue; + return pickledDataRet; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h index af763f4..7ed6d8b 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.h @@ -33,11 +33,11 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSessionKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer); -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsg, jobject aDecryptIndex, jobject aErrorMsg); +JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); +JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsg, jobject aDecryptIndex, jobject aErrorMsg); // serialization -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg); +JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg); JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initWithSerializedDataJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedData, jbyteArray aKey); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h index 7fea2f1..f1d124e 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h @@ -74,7 +74,6 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -jstring javaCStringToUtf8(JNIEnv *env, uint8_t *aCStringMsgPtr, size_t aMsgLength); #ifdef __cplusplus } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp index c649d32..9205fc0 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp @@ -213,45 +213,3 @@ jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_UTILITY); return instanceId; } - -/** -* Convert a C string into a UTF-8 format string. -* The conversion is performed in JAVA side to workaround the issue in NewStringUTF(). -* The problem is described here: https://github.com/eclipsesource/J2V8/issues/142 -*/ -jstring javaCStringToUtf8(JNIEnv *env, uint8_t *aCStringMsgPtr, size_t aMsgLength) -{ - jstring convertedRetValue = 0; - jbyteArray tempByteArray = NULL; - - if (!aCStringMsgPtr || !env) - { - LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)"); - } - else if (!(tempByteArray = env->NewByteArray(aMsgLength))) - { - LOGE("## javaCStringToUtf8(): failure - return byte array OOM"); - } - else - { - env->SetByteArrayRegion(tempByteArray, 0, aMsgLength, (const jbyte*)aCStringMsgPtr); - - // UTF-8 conversion from JAVA - jstring strEncode = (env)->NewStringUTF("UTF-8"); - jclass jClass = env->FindClass("java/lang/String"); - jmethodID cstor = env->GetMethodID(jClass, "<init>", "([BLjava/lang/String;)V"); - - if (jClass && strEncode) - { - convertedRetValue = (jstring) env->NewObject(jClass, cstor, tempByteArray, strEncode); - LOGD(" ## javaCStringToUtf8(): succeed"); - env->DeleteLocalRef(tempByteArray); - } - else - { - LOGE(" ## javaCStringToUtf8(): failure - invalid Java references"); - } - } - - return convertedRetValue; -} diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp index 0ccc6e8..211757f 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp @@ -138,12 +138,13 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)( /** * Get a base64-encoded identifier for this outbound group session. */ -JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) +JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) { LOGD("## sessionIdentifierJni(): outbound group session IN"); - OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); - jstring returnValueStr=0; + OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); + jbyteArray returnValue = 0; + if (!sessionPtr) { LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance"); @@ -172,8 +173,11 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE { // update length sessionIdPtr[result] = static_cast<char>('\0'); + + returnValue = env->NewByteArray(result); + 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)); - returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); } // free alloc @@ -181,7 +185,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE } } - return returnValueStr; + return returnValue; } @@ -215,11 +219,12 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, /** * Get the base64-encoded current ratchet key for this session.<br> */ -JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz) +JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz) { LOGD("## sessionKeyJni(): outbound group session IN"); + OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); - jstring returnValueStr = 0; + jbyteArray returnValue = 0; if (!sessionPtr) { @@ -250,7 +255,9 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env // update length 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)); - returnValueStr = env->NewStringUTF((const char*)sessionKeyPtr); + + returnValue = env->NewByteArray(result); + env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionKeyPtr); } // free alloc @@ -258,15 +265,15 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env } } - return returnValueStr; + return returnValue; } - -JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer) +JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer) { LOGD("## encryptMessageJni(): IN"); - jstring encryptedMsgRetValue = 0; + jbyteArray encryptedMsgRet = 0; + OlmOutboundGroupSession *sessionPtr = NULL; jbyte* clearMsgPtr = NULL; @@ -315,7 +322,9 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv encryptedMsgPtr[encryptedLength] = static_cast<char>('\0'); LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",static_cast<long unsigned int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr)); - encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr); + + encryptedMsgRet = env->NewByteArray(encryptedLength); + env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr); } free(encryptedMsgPtr); @@ -328,7 +337,7 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv env->ReleaseByteArrayElements(aClearMsgBuffer, clearMsgPtr, JNI_ABORT); } - return encryptedMsgRetValue; + return encryptedMsgRet; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h index 43ec4f7..71ddb3b 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h @@ -33,11 +33,11 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz); JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); +JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(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 jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer); +JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer); // serialization JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp index 491795f..5ae68d0 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp @@ -580,9 +580,10 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz * @param aEncryptedMsg message to decrypt * @return decrypted message if operation succeed, null otherwise */ -JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg) +JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg) { - jstring decryptedMsgRetValue = 0; + jbyteArray decryptedMsgRet = 0; + jclass encryptedMsgJClass = 0; jstring encryptedMsgJstring = 0; // <= obtained from encryptedMsgFieldId // field IDs @@ -668,16 +669,10 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t } else { - decryptedMsgRetValue = javaCStringToUtf8(env, plainTextMsgPtr, plaintextLength); + decryptedMsgRet = env->NewByteArray(plaintextLength); + env->SetByteArrayRegion(decryptedMsgRet, 0 , plaintextLength, (jbyte*)plainTextMsgPtr); - if (!decryptedMsgRetValue) - { - LOGE(" ## decryptMessageJni(): UTF-8 Conversion failure - javaCStringToUtf8() returns null"); - } - else - { - LOGD(" ## decryptMessageJni(): UTF-8 Conversion - decrypted returnedLg=%lu OK",static_cast<long unsigned int>(plaintextLength)); - } + LOGD(" ## decryptMessageJni(): UTF-8 Conversion - decrypted returnedLg=%lu OK",static_cast<long unsigned int>(plaintextLength)); } } } @@ -698,7 +693,7 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t free(plainTextMsgPtr); } - return decryptedMsgRetValue; + return decryptedMsgRet; } @@ -706,9 +701,9 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject t * Get the session identifier for this session. * @return the session identifier if operation succeed, null otherwise */ -JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz) +JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz) { - jstring returnValueStr=0; + jbyteArray returnValue = 0; LOGD("## getSessionIdentifierJni(): IN "); @@ -744,13 +739,16 @@ JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, job (static_cast<char*>(sessionIdPtr))[result] = static_cast<char>('\0'); LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%s",static_cast<long unsigned int>(result), (char*)sessionIdPtr); - returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); + + returnValue = env->NewByteArray(result); + env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); } + free(sessionIdPtr); } } - return returnValueStr; + return returnValue; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h index b18ac86..924b439 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.h @@ -45,9 +45,9 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *e // encrypt/decrypt JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg); -JNIEXPORT jstring 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 jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz); +JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz); // serialization JNIEXPORT jstring OLM_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg); diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp index 584e83e..f4a0c80 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp @@ -166,9 +166,10 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, j * @param aMessage * @return digest of the message if operation succeed, null otherwise **/ -JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHashBuffer) +JNIEXPORT jbyteArray OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHashBuffer) { - jstring sha256RetValue = 0; + jbyteArray sha256Ret = 0; + OlmUtility* utilityPtr = NULL; jbyte* messagePtr = NULL; @@ -212,9 +213,10 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jby { // update length (static_cast<char*>(hashValuePtr))[result] = static_cast<char>('\0'); - LOGD("## sha256Jni(): success - result=%lu hashValue=%s",static_cast<long unsigned int>(result), (char*)hashValuePtr); - sha256RetValue = env->NewStringUTF((const char*)hashValuePtr); + + sha256Ret = env->NewByteArray(result); + env->SetByteArrayRegion(sha256Ret, 0 , result, (jbyte*)hashValuePtr); } free(hashValuePtr); @@ -226,5 +228,5 @@ JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jby env->ReleaseByteArrayElements(aMessageToHashBuffer, messagePtr, JNI_ABORT); } - return sha256RetValue; + return sha256Ret; }
\ No newline at end of file diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h index 7c3601d..16d9e7a 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h @@ -30,7 +30,7 @@ extern "C" { 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, jbyteArray aSignature, jbyteArray aKey, jbyteArray aMessage); -JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHash); +JNIEXPORT jbyteArray OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHash); #ifdef __cplusplus } #endif |