From f2ca1ce304deee9e8e858ea4a71ea22ba38a342b Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Tue, 11 Oct 2016 15:53:49 +0200 Subject: - Add OlmSession unit test - Simplify JNI function signatures definition (Account & Session) --- .../src/main/java/org/matrix/olm/OlmAccount.java | 26 ++--- .../src/main/java/org/matrix/olm/OlmSession.java | 33 ++++--- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp | 31 +++--- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.h | 3 + .../OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h | 2 + .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp | 105 ++++++++++++--------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.h | 4 +- .../OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp | 2 +- 8 files changed, 124 insertions(+), 82 deletions(-) (limited to 'java/android/OlmLibSdk/olm-sdk/src/main') 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 156ec1a..e9eddc7 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 @@ -29,16 +29,12 @@ public class OlmAccount { public static final String JSON_KEY_IDENTITY_KEY = "curve25519"; public static final String JSON_KEY_FINGER_PRINT_KEY = "ed25519"; - /** instance unique identifier, used in JNI to match the corresponding native class **/ - private int mJavaInstanceId; - /** account raw pointer value (OlmAccount*) returned by JNI. * this value identifies uniquely the native account instance. */ private long mNativeOlmAccountId; public OlmAccount() { - mJavaInstanceId = hashCode(); //initNewAccount(); } @@ -101,8 +97,13 @@ public class OlmAccount { /** * Return the identity keys (identity & fingerprint keys) in a JSON array.
- * Public API for {@link #identityKeysJni()}. - * @return identity keys in JSON array format if operation succeed, null otherwise + * Public API for {@link #identityKeysJni()}.
+ * Ex: + * { + * "curve25519":"Vam++zZPMqDQM6ANKpO/uAl5ViJSHxV9hd+b0/fwRAg", + * "ed25519":"+v8SOlOASFTMrX3MCKBM4iVnYoZ+JIjpNt1fi8Z9O2I" + * } + * @return identity keys in JSON array if operation succeed, null otherwise */ public JSONObject identityKeys() { JSONObject identityKeysJsonObj = null; @@ -150,6 +151,14 @@ public class OlmAccount { /** * Return the "one time keys" in a JSON array.
* The number of "one time keys", is specified by {@link #generateOneTimeKeys(int)}
+ * Ex: + * { "curve25519": + * { + * "AAAABQ":"qefVZd8qvjOpsFzoKSAdfUnJVkIreyxWFlipCHjSQQg", + * "AAAABA":"/X8szMU+p+lsTnr56wKjaLgjTMQQkCk8EIWEAilZtQ8", + * "AAAAAw":"qxNxxFHzevFntaaPdT0fhhO7tc7pco4+xB/5VRG81hA", + * } + * }
* Public API for {@link #oneTimeKeysJni()}.
* Note: these keys are to be published on the server. * @return one time keys in JSON array format if operation succeed, null otherwise @@ -192,9 +201,4 @@ public class OlmAccount { * @return the signed message if operation succeed, null otherwise */ public native String signMessage(String aMessage); - - @Override - public String toString() { - return super.toString(); - } } 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 24a4215..0c2888c 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 @@ -19,11 +19,9 @@ package org.matrix.olm; import android.text.TextUtils; import android.util.Log; -import org.json.JSONException; -import org.json.JSONObject; - public class OlmSession { private static final String LOG_TAG = "OlmSession"; + /** session raw pointer value (OlmSession*) returned by JNI. * this value uniquely identifies the native session instance. **/ @@ -44,6 +42,14 @@ public class OlmSession { return mNativeOlmSessionId; } + /** + * Getter on the session ID. + * @return native session ID + */ + public OlmAccount getOlmAccountId(){ + return mOlmAccount; + } + /** * Destroy the corresponding OLM session native object.
* This method must ALWAYS be called when this JAVA instance @@ -105,8 +111,9 @@ public class OlmSession { // set the account of this session mOlmAccount = aAccount; - int retCode = initOutboundSessionJni(mOlmAccount.getOlmAccountId(), aTheirIdentityKey, aTheirOneTimeKey); - retObj = this; + if(0 == initOutboundSessionJni(mOlmAccount.getOlmAccountId(), aTheirIdentityKey, aTheirOneTimeKey)) { + retObj = this; + } } return retObj; @@ -121,7 +128,7 @@ public class OlmSession { * Public API for {@link #initInboundSessionJni(long, String)}. * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). * @param aAccount the account to associate with this session - * @param aOneTimeKeyMsg PRE KEY message TODO TBC + * @param aOneTimeKeyMsg PRE KEY message * @return this if operation succeed, null otherwise */ public OlmSession initInboundSessionWithAccount(OlmAccount aAccount, String aOneTimeKeyMsg) { @@ -227,6 +234,7 @@ public class OlmSession { /** * Encrypt a message using the session.
+ * The encrypted message is returned in a OlmMessage object. * Public API for {@link #encryptMessageJni(String, OlmMessage)}. * @param aClearMsg message to encrypted * @return the encrypted message if operation succeed, null otherwise @@ -243,11 +251,12 @@ public class OlmSession { private native int encryptMessageJni(String aClearMsg, OlmMessage aEncryptedMsg); - - @Override - public String toString() { - return super.toString(); - } - + /** + * Decrypt a message using the session.
+ * The encrypted message is given as a OlmMessage object. + * @param aEncryptedMsg message to decrypt + * @return the decrypted message if operation succeed, null otherwise + */ + public native String decryptMessage(OlmMessage aEncryptedMsg); } 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 ac48fd2..e663fc9 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 @@ -44,7 +44,7 @@ OlmAccount* initializeAccountMemory() * This method MUST be called when java counter part account instance is done. * */ -JNIEXPORT void JNICALL Java_org_matrix_olm_OlmAccount_releaseAccountJni(JNIEnv *env, jobject thiz) +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz) { OlmAccount* accountPtr = NULL; @@ -55,8 +55,11 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmAccount_releaseAccountJni(JNIEnv * LOGE("## releaseAccountJni(): failure - invalid Account ptr=NULL"); } else - { // even if free(NULL) does not crash, a test is performed for debug purpose + { + olm_clear_account(accountPtr); + LOGD("## releaseAccountJni(): IN"); + // even if free(NULL) does not crash, logs are performed for debug purpose free(accountPtr); LOGD("## releaseAccountJni(): OUT"); } @@ -68,7 +71,7 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmAccount_releaseAccountJni(JNIEnv * * to make the cast (OlmAccount* => jlong) platform independant. * @return the initialized OlmAccount* instance if init succeed, NULL otherwise **/ -JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmAccount_initNewAccountJni(JNIEnv *env, jobject thiz) +JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz) { OlmAccount *accountPtr = NULL; uint8_t *randomBuffPtr = NULL; @@ -120,7 +123,7 @@ JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmAccount_initNewAccountJni(JNIEnv * The keys are returned in the byte array. * @return a valid byte array if operation succeed, null otherwise **/ -JNIEXPORT jbyteArray JNICALL Java_org_matrix_olm_OlmAccount_identityKeysJni(JNIEnv *env, jobject thiz) +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject thiz) { OlmAccount* accountPtr = NULL; size_t identityKeysLength; @@ -175,7 +178,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_matrix_olm_OlmAccount_identityKeysJni(JNIE * Get the maximum number of "one time keys" the account can store. * **/ -JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmAccount_maxOneTimeKeys(JNIEnv *env, jobject thiz) +JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeys)(JNIEnv *env, jobject thiz) { OlmAccount* accountPtr = NULL; size_t maxKeys = -1; @@ -198,7 +201,7 @@ JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmAccount_maxOneTimeKeys(JNIEnv *en * @param aNumberOfKeys number of keys to generate * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise **/ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_generateOneTimeKeys(JNIEnv *env, jobject thiz, jint aNumberOfKeys) +JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeys)(JNIEnv *env, jobject thiz, jint aNumberOfKeys) { OlmAccount *accountPtr = NULL; uint8_t *randomBufferPtr = NULL; @@ -206,7 +209,6 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_generateOneTimeKeys(JNIEnv size_t randomLength; size_t result; - LOGD("## generateOneTimeKeys(): accountPtr =%p aNumberOfKeys=%d",accountPtr, aNumberOfKeys); if(NULL == (accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) { @@ -222,7 +224,10 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_generateOneTimeKeys(JNIEnv LOGE("## generateOneTimeKeys(): failure - random buffer init"); } else - { // retrieve key pairs in keysBytesPtr + { + LOGD("## generateOneTimeKeys(): accountPtr =%p aNumberOfKeys=%d",accountPtr, aNumberOfKeys); + + // retrieve key pairs in keysBytesPtr result = olm_account_generate_one_time_keys(accountPtr, aNumberOfKeys, (void*)randomBufferPtr, randomLength); if(result == olm_error()) { const char *errorMsgPtr = olm_account_last_error(accountPtr); @@ -249,7 +254,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_generateOneTimeKeys(JNIEnv * Return the public parts of the unpublished "one time keys" for the account * @return a valid byte array if operation succeed, null otherwise **/ -JNIEXPORT jbyteArray JNICALL Java_org_matrix_olm_OlmAccount_oneTimeKeysJni(JNIEnv *env, jobject thiz) +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz) { OlmAccount* accountPtr = NULL; size_t keysLength; @@ -303,7 +308,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_matrix_olm_OlmAccount_oneTimeKeysJni(JNIEn * @param aNativeOlmSessionId session instance * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS if no matching keys, ERROR_CODE_KO otherwise **/ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_removeOneTimeKeysForSession(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId) +JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSession)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId) { jint retCode = ERROR_CODE_KO; OlmAccount* accountPtr = NULL; @@ -342,7 +347,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_removeOneTimeKeysForSessio * Mark the current set of "one time keys" as being published. * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise **/ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_markOneTimeKeysAsPublished(JNIEnv *env, jobject thiz) +JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublished)(JNIEnv *env, jobject thiz) { jint retCode = ERROR_CODE_OK; OlmAccount* accountPtr = NULL; @@ -377,7 +382,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_markOneTimeKeysAsPublished * @param aMessage message to sign * @return the signed message, null otherwise **/ -JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmAccount_signMessage(JNIEnv *env, jobject thiz, jstring aMessage) +JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessage)(JNIEnv *env, jobject thiz, jstring aMessage) { OlmAccount* accountPtr = NULL; size_t signatureLength; @@ -439,7 +444,7 @@ JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmAccount_signMessage(JNIEnv *env } -JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmManager_getOlmLibVersion(JNIEnv* env, jobject thiz) +JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersion)(JNIEnv* env, jobject thiz) { uint8_t majorVer=0, minorVer=0, patchVer=0; jstring returnValueStr=0; 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 9c32912..33fc65c 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 @@ -3,6 +3,9 @@ #include "olm_jni.h" +#define OLM_ACCOUNT_FUNC_DEF(func_name) FUNC_DEF(OlmAccount,func_name) +#define OLM_MANAGER_FUNC_DEF(func_name) FUNC_DEF(OlmManager,func_name) + #ifdef __cplusplus extern "C" { #endif 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 1e85cc1..d811c3f 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 @@ -28,6 +28,8 @@ #define LOGE(...) #endif +#define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name + // Error codes definition static const int ERROR_CODE_OK = 0; static const int ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS = ERROR_CODE_OK+1; 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 dae905d..0febc02 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 @@ -41,7 +41,7 @@ OlmSession* initializeSessionMemory() return sessionPtr; } -JNIEXPORT void JNICALL Java_org_matrix_olm_OlmSession_releaseSessionJni(JNIEnv *env, jobject thiz) +JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) { OlmSession* sessionPtr = NULL; @@ -50,8 +50,11 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmSession_releaseSessionJni(JNIEnv * LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL"); } else - { // even if free(NULL) does not crash, a test is performed for debug purpose + { + olm_clear_session(sessionPtr); + LOGD("## releaseSessionJni(): IN"); + // even if free(NULL) does not crash, logs are performed for debug purpose free(sessionPtr); LOGD("## releaseSessionJni(): OUT"); } @@ -63,7 +66,7 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmSession_releaseSessionJni(JNIEnv * * to make the cast (OlmSession* => jlong) platform independent. * @return the initialized OlmSession* instance if init succeed, NULL otherwise **/ -JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmSession_initNewSessionJni(JNIEnv *env, jobject thiz) +JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) { OlmSession* sessionPtr = NULL; @@ -92,7 +95,7 @@ JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmSession_initNewSessionJni(JNIEnv * @param aTheirOneTimeKey the one time key of the recipient * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise **/ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initOutboundSessionJni(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jstring aTheirIdentityKey, jstring aTheirOneTimeKey) +JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jstring aTheirIdentityKey, jstring aTheirOneTimeKey) { jint retCode = ERROR_CODE_KO; OlmSession* sessionPtr = NULL; @@ -117,7 +120,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initOutboundSessionJni(JNI else { // allocate random buffer size_t randomSize = olm_create_outbound_session_random_length(sessionPtr); - if(false == setRandomInBuffer(&randomBuffPtr, randomSize)) + if((0!=randomSize) && (false == setRandomInBuffer(&randomBuffPtr, randomSize))) { LOGE("## initOutboundSessionJni(): failure - random buffer init"); } @@ -188,7 +191,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initOutboundSessionJni(JNI * @param aOneTimeKeyMsg PRE_KEY message * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise */ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionJni(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jstring aOneTimeKeyMsg) +JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jstring aOneTimeKeyMsg) { jint retCode = ERROR_CODE_KO; OlmSession *sessionPtr = NULL; @@ -206,7 +209,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionJni(JNIE } else if(0==aOneTimeKeyMsg) { - LOGE("## initOutboundSessionJni(): failure - invalid message"); + LOGE("## initInboundSessionJni(): failure - invalid message"); } else { // convert message to C strings @@ -217,7 +220,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionJni(JNIE else { int messageLength = env->GetStringUTFLength(aOneTimeKeyMsg); - LOGD("## initInboundSessionJni(): message=%s messageLength=%d",messagePtr,messageLength); + LOGD("## initInboundSessionJni(): messageLength=%d message=%s", messageLength, messagePtr); sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength); if(sessionResult == olm_error()) { @@ -245,7 +248,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionJni(JNIE * @param aOneTimeKeyMsg encrypted message * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise */ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionFromIdKeyJni(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jstring aTheirIdentityKey, jstring aOneTimeKeyMsg) +JNIEXPORT jint OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jstring aTheirIdentityKey, jstring aOneTimeKeyMsg) { jint retCode = ERROR_CODE_KO; OlmSession *sessionPtr = NULL; @@ -268,7 +271,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionFromIdKe } else if(0==aOneTimeKeyMsg) { - LOGE("## initOutboundSessionJni(): failure - invalid one time key message"); + LOGE("## initInboundSessionJni(): failure - invalid one time key message"); } else if(NULL == (messagePtr = env->GetStringUTFChars(aOneTimeKeyMsg, 0))) { @@ -316,7 +319,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionFromIdKe * @param aOneTimeKeyMsg PRE KEY message * @return ERROR_CODE_OK if match, ERROR_CODE_KO otherwise */ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_matchesInboundSessionJni(JNIEnv *env, jobject thiz, jstring aOneTimeKeyMsg) +JNIEXPORT jint OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobject thiz, jstring aOneTimeKeyMsg) { jint retCode = ERROR_CODE_KO; OlmSession *sessionPtr = NULL; @@ -367,7 +370,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_matchesInboundSessionJni(J * @param aOneTimeKeyMsg PRE KEY message * @return ERROR_CODE_OK if match, ERROR_CODE_KO otherwise */ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_matchesInboundSessionFromIdKeyJni(JNIEnv *env, jobject thiz, jstring aTheirIdentityKey, jstring aOneTimeKeyMsg) +JNIEXPORT jint JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jstring aTheirIdentityKey, jstring aOneTimeKeyMsg) { jint retCode = ERROR_CODE_KO; OlmSession *sessionPtr = NULL; @@ -432,7 +435,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_matchesInboundSessionFromI * @param [out] aEncryptedMsg ciphered message * @return ERROR_CODE_OK if encrypt operation succeed, ERROR_CODE_KO otherwise */ -JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_encryptMessageJni(JNIEnv *env, jobject thiz, jstring aClearMsg, jobject aEncryptedMsg) +JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsg, jobject aEncryptedMsg) { jint retCode = ERROR_CODE_KO; OlmSession *sessionPtr = NULL; @@ -467,15 +470,21 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_encryptMessageJni(JNIEnv * { LOGE("## encryptMessageJni(): failure - unable to get message field"); } - else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","I"))) + else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) { LOGE("## encryptMessageJni(): failure - unable to get message type field"); } else { + // get message type + size_t messageType = olm_encrypt_message_type(sessionPtr); + // compute random buffer + // Note: olm_encrypt_random_length() can return 0, which means + // it just does not need new random data to encrypt a new message size_t randomLength = olm_encrypt_random_length(sessionPtr); - if(false == setRandomInBuffer(&randomBuffPtr, randomLength)) + LOGD("## encryptMessageJni(): messageType=%lu randomLength=%lu",messageType,randomLength); + if( (0!=randomLength) && (false == setRandomInBuffer(&randomBuffPtr, randomLength))) { LOGE("## encryptMessageJni(): failure - random buffer init"); } @@ -486,10 +495,16 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_encryptMessageJni(JNIEnv * size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength); if(NULL == (encryptedMsgPtr = (void*)malloc(encryptedMsgLength*sizeof(uint8_t)))) { - LOGE("## encryptMessageJni(): failure - random buffer OOM"); + LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); } else - { // encrypt message + { + if(0==randomLength) + { + LOGW("## encryptMessageJni(): random buffer is not required"); + } + + // encrypt message size_t result = olm_encrypt(sessionPtr, (void const *)clearMsgPtr, clearMsgLength, @@ -505,13 +520,11 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_encryptMessageJni(JNIEnv * else { // update message type: PRE KEY or normal - size_t messageType = olm_encrypt_message_type(sessionPtr); env->SetLongField(aEncryptedMsg, typeMsgFieldId, (jlong)messageType); // update message: encryptedMsgPtr => encryptedJstring jstring encryptedJstring = env->NewStringUTF((const char*)encryptedMsgPtr); env->SetObjectField(aEncryptedMsg, encryptedMsgFieldId, (jobject)encryptedJstring); - // TODO mem leak: check if free(encryptedMsgPtr); does not interfer with line above retCode = ERROR_CODE_OK; LOGD("## encryptMessageJni(): success - result=%lu Type=%lu encryptedMsg=%s", result, messageType, (const char*)encryptedMsgPtr); @@ -541,11 +554,11 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_encryptMessageJni(JNIEnv * /** - * Decrypt a message using the session. to a base64 ciphertext.
+ * Decrypt a message using the session.
* @param aEncryptedMsg message to decrypt * @return decrypted message if operation succeed, null otherwise */ -JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_decryptMessageJni(JNIEnv *env, jobject thiz, jobject aEncryptedMsg) +JNIEXPORT jstring OLM_SESSION_FUNC_DEF(decryptMessage)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg) { jstring decryptedMsgRetValue = 0; jclass encryptedMsgJclass = 0; @@ -556,37 +569,37 @@ JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_decryptMessageJni(JNIEn // ptrs OlmSession *sessionPtr = NULL; const char *encryptedMsgPtr = NULL; // <= obtained from encryptedMsgJstring - void *decryptedMsgPtr = NULL; + void *plainTextMsgPtr = NULL; char *tempEncryptedPtr = NULL; if(NULL == (sessionPtr = (OlmSession*)getSessionInstanceId(env,thiz))) { - LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL"); + LOGE("## decryptMessage(): failure - invalid Session ptr=NULL"); } else if(0 == aEncryptedMsg) { - LOGE("## decryptMessageJni(): failure - invalid clear message"); + LOGE("## decryptMessage(): failure - invalid clear message"); } else if(0 == (encryptedMsgJclass = env->GetObjectClass(aEncryptedMsg))) { - LOGE("## decryptMessageJni(): failure - unable to get crypted message class"); + LOGE("## decryptMessage(): failure - unable to get crypted message class"); } else if(0 == (encryptedMsgFieldId = env->GetFieldID(encryptedMsgJclass,"mCipherText","Ljava/lang/String;"))) { - LOGE("## decryptMessageJni(): failure - unable to get message field"); + LOGE("## decryptMessage(): failure - unable to get message field"); } - else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJclass,"mType","I"))) + else if(0 == (typeMsgFieldId = env->GetFieldID(encryptedMsgJclass,"mType","J"))) { - LOGE("## decryptMessageJni(): failure - unable to get message type field"); + LOGE("## decryptMessage(): failure - unable to get message type field"); } else if(0 == (encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId))) { - LOGE("## decryptMessageJni(): failure - JNI encrypted object "); + LOGE("## decryptMessage(): failure - JNI encrypted object "); } else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0))) { - LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); + LOGE("## decryptMessage(): failure - encrypted message JNI allocation OOM"); } else { @@ -596,43 +609,47 @@ JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_decryptMessageJni(JNIEn size_t encryptedMsgLength = env->GetStringUTFLength(encryptedMsgJstring); // create a dedicated temp buffer to be used in next Olm API calls - tempEncryptedPtr = (char*)malloc(encryptedMsgLength*sizeof(uint8_t)); + tempEncryptedPtr = static_cast(malloc(encryptedMsgLength*sizeof(uint8_t))); memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); LOGD("## decryptMessageJni(): encryptedMsgType=%lld encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgType,encryptedMsgLength,encryptedMsgPtr); // get max plaintext length - size_t maxPlaintextLength = olm_decrypt_max_plaintext_length(sessionPtr, + size_t maxPlainTextLength = olm_decrypt_max_plaintext_length(sessionPtr, encryptedMsgType, - (void*)tempEncryptedPtr, + static_cast(tempEncryptedPtr), encryptedMsgLength); - // Note: tempEncryptedPtr was destroyed by olm_decrypt_max_plaintext_length() + // Note: tempEncryptedPtr is destroyed by olm_decrypt_max_plaintext_length() - if(maxPlaintextLength == olm_error()) + if(maxPlainTextLength == olm_error()) { const char *errorMsgPtr = olm_session_last_error(sessionPtr); LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); } else { + LOGD("## decryptMessage(): maxPlaintextLength=%lu",maxPlainTextLength); + // allocate output decrypted message - decryptedMsgPtr = (void*)malloc(maxPlaintextLength*sizeof(uint8_t)); + plainTextMsgPtr = static_cast(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); size_t plaintextLength = olm_decrypt(sessionPtr, encryptedMsgType, (void*)encryptedMsgPtr, encryptedMsgLength, - (void*)decryptedMsgPtr, - maxPlaintextLength); + (void*)plainTextMsgPtr, + maxPlainTextLength); if(plaintextLength == olm_error()) { const char *errorMsgPtr = olm_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s",errorMsgPtr); + LOGE("## decryptMessage(): failure - olm_decrypt Msg=%s",errorMsgPtr); } else { - decryptedMsgRetValue = env->NewStringUTF((const char*)decryptedMsgPtr); + (static_cast(plainTextMsgPtr))[plaintextLength] = static_cast('\0'); + LOGD("## decryptMessage(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, static_cast(plainTextMsgPtr)); + decryptedMsgRetValue = env->NewStringUTF(static_cast(plainTextMsgPtr)); } } } @@ -648,9 +665,9 @@ JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_decryptMessageJni(JNIEn free(tempEncryptedPtr); } - if(NULL != decryptedMsgPtr) + if(NULL != plainTextMsgPtr) { - free(decryptedMsgPtr); + free(plainTextMsgPtr); } return decryptedMsgRetValue; @@ -663,7 +680,7 @@ JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_decryptMessageJni(JNIEn * Get the session identifier for this session. * @return the session identifier if operation succeed, null otherwise */ -JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_getSessionIdentifierJni(JNIEnv *env, jobject thiz) +JNIEXPORT jstring OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz) { OlmSession *sessionPtr = NULL; void *sessionIdPtr = NULL; 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 b04e71e..9b3c30f 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 @@ -3,6 +3,8 @@ #include "olm_jni.h" +#define OLM_SESSION_FUNC_DEF(func_name) FUNC_DEF(OlmSession,func_name) + #ifdef __cplusplus extern "C" { #endif @@ -24,7 +26,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_matchesInboundSessionFromI // encrypt/decrypt JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_encryptMessageJni(JNIEnv *env, jobject thiz, jstring aClearMsg, jobject aEncryptedMsg); -JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_decryptMessageJni(JNIEnv *env, jobject thiz, jobject aEncryptedMsg); +JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_decryptMessage(JNIEnv *env, jobject thiz, jobject aEncryptedMsg); JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmSession_getSessionIdentifierJni(JNIEnv *env, jobject thiz); 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 4f96e10..99df7f5 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 @@ -118,7 +118,7 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) { if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmSessionId", "J"))) { - instanceId = aJniEnv->GetIntField(aJavaObject, instanceIdField); + instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); aJniEnv->DeleteLocalRef(loaderClass); } else -- cgit v1.2.3