From ebfcd03ce5b627236edf2c2c1f334f5c59f9d291 Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Fri, 14 Oct 2016 18:43:57 +0200 Subject: - Add in/outbound group unit test OK --- .../src/main/java/org/matrix/olm/OlmException.java | 35 ++++ .../org/matrix/olm/OlmInboundGroupSession.java | 8 +- .../org/matrix/olm/OlmOutboundGroupSession.java | 9 +- .../java/org/matrix/olm/OlmUtilsException.java | 35 ---- .../src/main/jni/olm_inbound_group_session.cpp | 65 ++++--- .../src/main/jni/olm_outbound_group_session.cpp | 61 +++--- .../OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp | 6 +- .../OlmLibSdk/olm-sdk/src/main/jni/olm_utility.cpp | 206 ++------------------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_utility.h | 7 +- 9 files changed, 130 insertions(+), 302 deletions(-) create mode 100644 java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java delete mode 100644 java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java (limited to 'java/android/OlmLibSdk/olm-sdk/src/main') diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java new file mode 100644 index 0000000..d4c642f --- /dev/null +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java @@ -0,0 +1,35 @@ +/* + * Copyright 2016 OpenMarket Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.matrix.olm; + +public class OlmException extends Exception { + // exception codes + public static final int EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE = 0; + public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 1; + public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 2; + + private final int mCode; + + public OlmException(int aExceptionCode) { + super(); + mCode = aExceptionCode; + } + + public int getExceptionCode() { + return mCode; + } +} 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 aa15c32..86f86c4 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 @@ -48,16 +48,16 @@ public class OlmInboundGroupSession implements Serializable { * The session key parameter is retrieved from a outbound group session * See {@link #initNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)} * @param aSessionKey session key - * @throws OlmUtilsException + * @throws OlmException */ - public OlmInboundGroupSession(String aSessionKey) throws OlmUtilsException { + public OlmInboundGroupSession(String aSessionKey) throws OlmException { if(initNewSession()) { if( 0 != initInboundGroupSessionWithSessionKey(aSessionKey)) { releaseSession();// prevent memory leak before throwing - throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION); + throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION); } } else { - throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE); + throw new OlmException(OlmException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE); } } 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 bbe2718..bca7ab3 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 @@ -18,7 +18,6 @@ package org.matrix.olm; import android.text.TextUtils; -import android.util.Log; public class OlmOutboundGroupSession { private static final String LOG_TAG = "OlmOutboundGroupSession"; @@ -41,16 +40,16 @@ public class OlmOutboundGroupSession { * Create and save a new session native instance ID and * initialise a new outbound group session.
* See {@link #initNewSession()} and {@link #initOutboundGroupSession()} - * @throws OlmUtilsException + * @throws OlmException */ - public OlmOutboundGroupSession() throws OlmUtilsException { + public OlmOutboundGroupSession() throws OlmException { if(initNewSession()) { if( 0 != initOutboundGroupSession()) { releaseSession();// prevent memory leak before throwing - throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION); + throw new OlmException(OlmException.EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION); } } else { - throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE); + throw new OlmException(OlmException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE); } } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java deleted file mode 100644 index f0cdc83..0000000 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtilsException.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -public class OlmUtilsException extends Exception { - // exception codes - public static final int EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE = 0; - public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 1; - public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 2; - - private final int mCode; - - public OlmUtilsException(int aExceptionCode) { - super(); - mCode = aExceptionCode; - } - - public int getExceptionCode() { - return mCode; - } -} 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 16e5a0b..09995ab 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 @@ -26,7 +26,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env { OlmInboundGroupSession* sessionPtr = NULL; - LOGD("## releaseSessionJni(): sessionPtr=%p",sessionPtr); + LOGD("## releaseSessionJni(): intbound group session"); if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) { @@ -34,12 +34,14 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env } else { + LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); + size_t retCode = olm_clear_inbound_group_session(sessionPtr); - LOGD("## releaseSessionJni(): clear_inbound_group_session=%lu",retCode); + LOGD(" ## releaseSessionJni(): clear_inbound_group_session=%lu",retCode); - LOGD("## releaseSessionJni(): IN"); + LOGD(" ## releaseSessionJni(): free IN"); free(sessionPtr); - LOGD("## releaseSessionJni(): OUT"); + LOGD(" ## releaseSessionJni(): free OUT"); } } @@ -52,20 +54,23 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) { OlmInboundGroupSession* sessionPtr = NULL; - size_t sessionSize = olm_inbound_group_session_size(); + size_t sessionSize = 0; + + LOGD("## initNewSessionJni(): inbound group session IN"); + sessionSize = olm_inbound_group_session_size(); if(0 == sessionSize) { - LOGE("## initNewSessionJni(): failure - inbound group session size = 0"); + LOGE(" ## initNewSessionJni(): failure - inbound group session size = 0"); } else if(NULL != (sessionPtr=(OlmInboundGroupSession*)malloc(sessionSize))) { sessionPtr = olm_inbound_group_session(sessionPtr); - LOGD("## initNewSessionJni(): success - inbound group session size=%lu",sessionSize); + LOGD(" ## initNewSessionJni(): success - inbound group session size=%lu",sessionSize); } else { - LOGE("## initNewSessionJni(): failure - inbound group session OOM"); + LOGE(" ## initNewSessionJni(): failure - inbound group session OOM"); } return (jlong)(intptr_t)sessionPtr; @@ -83,32 +88,34 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes const uint8_t *sessionKeyPtr = NULL; size_t sessionResult; + LOGD("## initInboundGroupSessionWithSessionKeyJni(): inbound group session IN"); + if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) { - LOGE("## initInboundGroupSessionWithSessionKeyJni(): failure - invalid inbound group session instance"); + LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid inbound group session instance"); } else if(0 == aSessionKey) { - LOGE("## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"); + LOGE(" ## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"); } else if(NULL == (sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0))) { - LOGE("## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM"); + LOGE(" ## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM"); } else { size_t sessionKeyLength = (size_t)env->GetStringUTFLength(aSessionKey); - LOGD("## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu",sessionKeyLength); + LOGD(" ## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu",sessionKeyLength); sessionResult = olm_init_inbound_group_session(sessionPtr, sessionKeyPtr, sessionKeyLength); if(sessionResult == olm_error()) { const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); + LOGE(" ## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); } else { retCode = ERROR_CODE_OK; - LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult); + LOGD(" ## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult); } } @@ -135,17 +142,17 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) { - LOGE("## sessionIdentifierJni(): failure - invalid inbound group session instance"); + LOGE(" ## sessionIdentifierJni(): failure - invalid inbound group session instance"); } else { // get the size to alloc size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr); - LOGD("## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",lengthSessionId); + LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",lengthSessionId); if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)))) { - LOGE("## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"); + LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"); } else { @@ -153,13 +160,13 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn if (result == olm_error()) { const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",errorMsgPtr); + LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",errorMsgPtr); } else { // update length sessionIdPtr[result] = static_cast('\0'); - LOGD("## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",result, (char*)sessionIdPtr); + LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",result, (char*)sessionIdPtr); returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); } free(sessionIdPtr); @@ -178,19 +185,19 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * uint8_t *plainTextMsgPtr = NULL; uint8_t *tempEncryptedPtr = NULL; - LOGD("## decryptMessageJni(): IN"); + LOGD("## decryptMessageJni(): inbound group session IN"); if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) { - LOGE("## decryptMessageJni(): failure - invalid inbound group session ptr=NULL"); + LOGE(" ## decryptMessageJni(): failure - invalid inbound group session ptr=NULL"); } else if(0 == aEncryptedMsg) { - LOGE("## decryptMessageJni(): failure - invalid encrypted message"); + LOGE(" ## decryptMessageJni(): failure - invalid encrypted message"); } else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0))) { - LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); + LOGE(" ## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); } else { @@ -200,12 +207,12 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * // create a dedicated temp buffer to be used in next Olm API calls if(NULL == (tempEncryptedPtr = static_cast(malloc(encryptedMsgLength*sizeof(uint8_t))))) { - LOGE("## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"); + LOGE(" ## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"); } else { memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - LOGD("## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgLength,encryptedMsgPtr); + LOGD(" ## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgLength,encryptedMsgPtr); // get max plaintext length size_t maxPlainTextLength = olm_group_decrypt_max_plaintext_length(sessionPtr, @@ -214,11 +221,11 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * if(maxPlainTextLength == olm_error()) { const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); + LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); } else { - LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",maxPlainTextLength); + LOGD(" ## decryptMessageJni(): maxPlaintextLength=%lu",maxPlainTextLength); // allocate output decrypted message plainTextMsgPtr = static_cast(malloc(maxPlainTextLength*sizeof(uint8_t))); @@ -233,14 +240,14 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv * if(plaintextLength == olm_error()) { const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr); + LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr); } else { // update decrypted buffer size plainTextMsgPtr[plaintextLength] = static_cast('\0'); - LOGD("## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, (char*)plainTextMsgPtr); + LOGD(" ## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, (char*)plainTextMsgPtr); decryptedMsgRetValue = env->NewStringUTF((const char*)plainTextMsgPtr); } } 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 46c0ee4..42d8901 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 @@ -26,20 +26,22 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en { OlmOutboundGroupSession* sessionPtr = NULL; - LOGD("## releaseSessionJni(): sessionPtr=%p",sessionPtr); + LOGD("## releaseSessionJni(): outbound group session"); if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## releaseSessionJni(): failure - invalid outbound group session instance"); + LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance"); } else { + LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); + size_t retCode = olm_clear_outbound_group_session(sessionPtr); - LOGD("## releaseSessionJni(): clear_outbound_group_session=%lu",retCode); + LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",retCode); - LOGD("## releaseSessionJni(): IN"); + LOGD(" ## releaseSessionJni(): free IN"); free(sessionPtr); - LOGD("## releaseSessionJni(): OUT"); + LOGD(" ## releaseSessionJni(): free OUT"); } } @@ -52,20 +54,23 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz) { OlmOutboundGroupSession* sessionPtr = NULL; - size_t sessionSize = olm_outbound_group_session_size(); + size_t sessionSize = 0; + + LOGD("## initNewSessionJni(): outbound group session IN"); + sessionSize = olm_outbound_group_session_size(); if(0 == sessionSize) { - LOGE("## initNewSessionJni(): failure - outbound group session size = 0"); + LOGE(" ## initNewSessionJni(): failure - outbound group session size = 0"); } else if(NULL != (sessionPtr=(OlmOutboundGroupSession*)malloc(sessionSize))) { sessionPtr = olm_outbound_group_session(sessionPtr); - LOGD("## initNewSessionJni(): success - outbound group session size=%lu",sessionSize); + LOGD(" ## initNewSessionJni(): success - outbound group session size=%lu",sessionSize); } else { - LOGE("## initNewSessionJni(): failure - outbound group session OOM"); + LOGE(" ## initNewSessionJni(): failure - outbound group session OOM"); } return (jlong)(intptr_t)sessionPtr; @@ -85,33 +90,33 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)( if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## initOutboundGroupSessionJni(): failure - invalid outbound group session instance"); + LOGE(" ## initOutboundGroupSessionJni(): failure - invalid outbound group session instance"); } else { // compute random buffer size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr); - LOGW("## initOutboundGroupSessionJni(): randomLength=%lu",randomLength); + LOGW(" ## initOutboundGroupSessionJni(): randomLength=%lu",randomLength); if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength)) { - LOGE("## initOutboundGroupSessionJni(): failure - random buffer init"); + LOGE(" ## initOutboundGroupSessionJni(): failure - random buffer init"); } else { if(0==randomLength) { - LOGW("## initOutboundGroupSessionJni(): random buffer is not required"); + LOGW(" ## initOutboundGroupSessionJni(): random buffer is not required"); } size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength); if(sessionResult == olm_error()) { const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); - LOGE("## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",errorMsgPtr); + LOGE(" ## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",errorMsgPtr); } else { retCode = ERROR_CODE_OK; - LOGD("## initOutboundGroupSessionJni(): success - result=%lu", sessionResult); + LOGD(" ## initOutboundGroupSessionJni(): success - result=%lu", sessionResult); } } } @@ -137,17 +142,17 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## sessionIdentifierJni(): failure - invalid outbound group session instance"); + LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance"); } else { // get the size to alloc size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr); - LOGD("## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",lengthSessionId); + LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",lengthSessionId); if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)))) { - LOGE("## sessionIdentifierJni(): failure - outbound identifier allocation OOM"); + LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM"); } else { @@ -155,13 +160,13 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE if (result == olm_error()) { const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); - LOGE("## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",errorMsgPtr); + LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",errorMsgPtr); } else { // update length sessionIdPtr[result] = static_cast('\0'); - LOGD("## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",result, (char*)sessionIdPtr); + LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",result, (char*)sessionIdPtr); returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); } @@ -189,13 +194,13 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## messageIndexJni(): failure - invalid outbound group session instance"); + LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance"); } else { indexRetValue = static_cast(olm_outbound_group_session_message_index(sessionPtr)); } - LOGD("## messageIndexJni(): success - index=%d",indexRetValue); + LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue); return indexRetValue; } @@ -282,28 +287,28 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength); if(NULL == (encryptedMsgPtr = (uint8_t*)malloc(encryptedMsgLength*sizeof(uint8_t)))) { - LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); + LOGE(" ## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); } else { LOGD(" ## encryptMessageJni(): estimated encryptedMsgLength=%lu",encryptedMsgLength); - size_t decryptedLength = olm_group_encrypt(sessionPtr, + size_t encryptedLength = olm_group_encrypt(sessionPtr, (uint8_t*)clearMsgPtr, clearMsgLength, encryptedMsgPtr, encryptedMsgLength); - if(decryptedLength == olm_error()) + if(encryptedLength == olm_error()) { const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr); + LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",errorMsgPtr); } else { // update decrypted buffer size - encryptedMsgPtr[decryptedLength] = static_cast('\0'); + encryptedMsgPtr[encryptedLength] = static_cast('\0'); - LOGD(" ## encryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",decryptedLength, (char*)encryptedMsgPtr); + LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%s",encryptedLength, (char*)encryptedMsgPtr); encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr); } } 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 5911591..889e993 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 @@ -69,14 +69,16 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thi { OlmSession* sessionPtr = NULL; + LOGD("## initNewSessionJni(): OlmSession IN"); + // init account memory allocation if(NULL == (sessionPtr = initializeSessionMemory())) { - LOGE("## initNewSessionJni(): failure - init session OOM"); + LOGE(" ## initNewSessionJni(): failure - init session OOM"); } else { - LOGD("## initNewSessionJni(): success - OLM session created"); + LOGD(" ## initNewSessionJni(): success - OLM session created"); } return (jlong)(intptr_t)sessionPtr; 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 bd920fe..fb9c9c5 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 @@ -1,6 +1,3 @@ -/** - * Created by pedrocon on 06/10/2016. - */ /* * Copyright 2016 OpenMarket Ltd * @@ -21,201 +18,20 @@ #include "olm_utility.h" /** -* Init a buffer with a given number of random values. -* @param aBuffer2Ptr the buffer to be initialized -* @param aRandomSize the number of random values to apply -* @return true if operation succeed, false otherwise -**/ -bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize) + * Verify an ed25519 signature. + * If the key was too small then the message will be "OLM.INVALID_BASE64". + * If the signature was invalid then the message will be "OLM.BAD_MESSAGE_MAC". + * @param aKey the ed25519 key. + * @param aMessage the message which was signed. + * @param aSignature the base64-encoded signature to be checked. + * @param the result error if there is a problem with the verification. + * @return true if validation succeed, false otherwise + */ +JNIEXPORT bool OLM_UTILITY_FUNC_DEF(ed25519VerifyJni)(JNIEnv *env, jobject thiz, jstring aKey, jstring aMessage, jstring aSignature, jobject aErrorMessage) { bool retCode = false; - if(NULL == aBuffer2Ptr) - { - LOGD("## setRandomInBuffer(): failure - aBuffer=NULL"); - } - else if(0 == aRandomSize) - { - LOGD("## setRandomInBuffer(): failure - random size=0"); - } - else if(NULL == (*aBuffer2Ptr = (uint8_t*)malloc(aRandomSize*sizeof(uint8_t)))) - { - LOGD("## setRandomInBuffer(): failure - alloc mem OOM"); - } - else - { - LOGD("## setRandomInBuffer(): randomSize=%ld",aRandomSize); - srand(time(NULL)); // init seed - for(size_t i=0;iGetObjectClass(aJavaObject))) - { - if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmAccountId", "J"))) - { - instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); - aJniEnv->DeleteLocalRef(loaderClass); - LOGD("## getAccountInstanceId(): read from java instanceId=%lld",instanceId); - } - else - { - LOGD("## getAccountInstanceId() ERROR! GetFieldID=null"); - } - } - else - { - LOGD("## getAccountInstanceId() ERROR! GetObjectClass=null"); - } - } - else - { - LOGD("## getAccountInstanceId() ERROR! aJniEnv=NULL"); - } - LOGD("## getAccountInstanceId() success - instanceId=%lld",instanceId); - return instanceId; -} - -/** -* Read the session instance ID of the calling object (aJavaObject).
-* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the instance ID if read succeed, -1 otherwise. -**/ -jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - jlong instanceId=-1; - jfieldID instanceIdField; - jclass loaderClass; - - if(NULL!=aJniEnv) - { - if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) - { - if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmSessionId", "J"))) - { - instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); - aJniEnv->DeleteLocalRef(loaderClass); - } - else - { - LOGD("## getSessionInstanceId() ERROR! GetFieldID=null"); - } - } - else - { - LOGD("## getSessionInstanceId() ERROR! GetObjectClass=null"); - } - } - else - { - LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL"); - } - - //LOGD("## getSessionInstanceId() success - instanceId=%lld",instanceId); - return instanceId; -} - - -/** -* Read the inbound group session instance ID of the calling object (aJavaObject).
-* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the instance ID if read succeed, -1 otherwise. -**/ -jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - jlong instanceId=-1; - jfieldID instanceIdField; - jclass loaderClass; - - if(NULL!=aJniEnv) - { - if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) - { - if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmInboundGroupSessionId", "J"))) - { - instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); - aJniEnv->DeleteLocalRef(loaderClass); - } - else - { - LOGD("## getSessionInstanceId() ERROR! GetFieldID=null"); - } - } - else - { - LOGD("## getSessionInstanceId() ERROR! GetObjectClass=null"); - } - } - else - { - LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL"); - } - - return instanceId; -} - - -/** -* Read the outbound group session instance ID of the calling object (aJavaObject).
-* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the instance ID if read succeed, -1 otherwise. -**/ -jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - jlong instanceId=-1; - jfieldID instanceIdField; - jclass loaderClass; - - if(NULL!=aJniEnv) - { - if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) - { - if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmOutboundGroupSessionId", "J"))) - { - instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); - aJniEnv->DeleteLocalRef(loaderClass); - } - else - { - LOGD("## getSessionInstanceId() ERROR! GetFieldID=null"); - } - } - else - { - LOGD("## getSessionInstanceId() ERROR! GetObjectClass=null"); - } - } - else - { - LOGD("## getSessionInstanceId() ERROR! aJniEnv=NULL"); - } - - return instanceId; -} +} \ 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 b16e915..4e5772c 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 @@ -17,19 +17,18 @@ #ifndef _OMLUTILITY_H #define _OMLUTILITY_H +#define OLM_UTILITY_FUNC_DEF(func_name) FUNC_DEF(OlmUtility,func_name) #ifdef __cplusplus extern "C" { #endif -bool setRandomInBuffer(uint8_t **aBuffer2Ptr, size_t aRandomSize); -jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); +JNIEXPORT bool OLM_UTILITY_FUNC_DEF(ed25519VerifyJni)(JNIEnv *env, jobject thiz, jstring aKey, jstring aMessage, jstring aSignature, jobject aErrorMessage); #ifdef __cplusplus } #endif + #endif -- cgit v1.2.3