aboutsummaryrefslogtreecommitdiff
path: root/java/android/OlmLibSdk/olm-sdk/src/main/jni
diff options
context:
space:
mode:
authorpedroGitt <pedro.contreiras@amdocs.com>2016-10-14 15:27:20 +0200
committerpedroGitt <pedro.contreiras@amdocs.com>2016-10-14 15:27:20 +0200
commit102809955026d92a3f5cf29e05998d91a992de9c (patch)
treeca8dae7dada59e283d0ccc85f2b299b49d53f4a3 /java/android/OlmLibSdk/olm-sdk/src/main/jni
parent57ec6fff885e25b32e749d3e63788ff010b1fdfd (diff)
- Add inbound and outbound group sessions
- Modify constructors for inbound and outbound group sessions - Add new Ecxception class
Diffstat (limited to 'java/android/OlmLibSdk/olm-sdk/src/main/jni')
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/Android.mk5
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp10
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp44
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h17
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp220
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp270
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h7
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp4
8 files changed, 431 insertions, 146 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/Android.mk b/java/android/OlmLibSdk/olm-sdk/src/main/jni/Android.mk
index 01c0dc9..92a9359 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/Android.mk
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/Android.mk
@@ -45,8 +45,9 @@ $(SRC_ROOT_DIR)/lib/crypto-algorithms/aes.c \
$(SRC_ROOT_DIR)/lib/curve25519-donna/curve25519-donna.c \
olm_account.cpp \
olm_session.cpp \
-olm_utility.cpp \
-olm_inbound_group_session.cpp
+olm_jni_helper.cpp \
+olm_inbound_group_session.cpp \
+olm_outbound_group_session.cpp
LOCAL_LDLIBS := -llog
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 ba15c13..860ff3e 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
@@ -15,7 +15,6 @@
*/
#include "olm_account.h"
-#include "olm_utility.h"
/**
* Init memory allocation for account creation.
@@ -85,9 +84,12 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
}
else
{
- // allocate random buffer
+ // get random buffer size
randomSize = olm_create_account_random_length(accountPtr);
- if(!setRandomInBuffer(&randomBuffPtr, randomSize))
+ LOGD("## initNewAccount(): randomSize=%lu", randomSize);
+
+ // allocate random buffer
+ if((0!=randomSize) && !setRandomInBuffer(&randomBuffPtr, randomSize))
{
LOGE("## initNewAccount(): failure - random buffer init");
}
@@ -219,7 +221,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeys)(JNIEnv *env, jobject th
randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys);
LOGD("## generateOneTimeKeys(): randomLength=%ld", randomLength);
- if(!setRandomInBuffer(&randomBufferPtr, randomLength))
+ if((0!=randomLength) && !setRandomInBuffer(&randomBufferPtr, randomLength))
{
LOGE("## generateOneTimeKeys(): failure - random buffer init");
}
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 43e9e20..16e5a0b 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
@@ -15,7 +15,6 @@
*/
#include "olm_inbound_group_session.h"
-#include "olm_utility.h"
/**
@@ -123,45 +122,54 @@ 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)
{
OlmInboundGroupSession *sessionPtr = NULL;
uint8_t *sessionIdPtr = NULL;
jstring returnValueStr=0;
- // get the size to alloc to contain the id
- size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr);
+ LOGD("## sessionIdentifierJni(): inbound group session IN");
if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{
LOGE("## sessionIdentifierJni(): failure - invalid inbound group session instance");
}
- else if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t))))
- {
- LOGE("## sessionIdentifierJni(): failure - identifier allocation OOM");
- }
else
{
- size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
- if (result == olm_error())
+ // get the size to alloc
+ size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr);
+ LOGD("## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",lengthSessionId);
+
+ if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t))))
{
- const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
- LOGE("## sessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr);
+ LOGE("## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM");
}
else
{
- // update length
- sessionIdPtr[result] = static_cast<char>('\0');
-
- LOGD("## sessionIdentifierJni(): success - result=%lu sessionId=%s",result, (char*)sessionIdPtr);
- returnValueStr = env->NewStringUTF((const char*)sessionIdPtr);
+ size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
+ 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);
+ }
+ else
+ {
+ // update length
+ sessionIdPtr[result] = static_cast<char>('\0');
+ LOGD("## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",result, (char*)sessionIdPtr);
+ returnValueStr = env->NewStringUTF((const char*)sessionIdPtr);
+ }
+ free(sessionIdPtr);
}
- free(sessionIdPtr);
}
return returnValueStr;
}
+
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg)
{
jstring decryptedMsgRetValue = 0;
@@ -178,7 +186,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
}
else if(0 == aEncryptedMsg)
{
- LOGE("## decryptMessageJni(): failure - invalid clear message");
+ LOGE("## decryptMessageJni(): failure - invalid encrypted message");
}
else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0)))
{
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 8f1555d..2bfb9f8 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
@@ -53,4 +53,21 @@ static const int ERROR_CODE_KO = -1;
// constants
static const int ACCOUNT_CREATION_RANDOM_MODULO = 256;
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// internal helper functions
+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);
+jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+
#endif
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
new file mode 100644
index 0000000..1d64d75
--- /dev/null
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp
@@ -0,0 +1,220 @@
+/**
+ * Created by pedrocon on 06/10/2016.
+ */
+/*
+ * 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.
+ */
+
+#include "olm_jni.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)
+{
+ 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;i<aRandomSize;i++)
+ {
+ (*aBuffer2Ptr)[i] = (uint8_t)(rand()%ACCOUNT_CREATION_RANDOM_MODULO);
+
+ // debug purpose
+ //LOGD("## setRandomInBuffer(): randomBuffPtr[%ld]=%d",i, (*aBuffer2Ptr)[i]);
+ }
+
+ retCode = true;
+ }
+ return retCode;
+}
+
+
+/**
+* Read the account instance ID of the calling object.
+* @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 operation succeed, -1 if instance ID was not found.
+**/
+jlong getAccountInstanceId(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, "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).<br>
+* @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).<br>
+* @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("## getInboundGroupSessionInstanceId() ERROR! GetFieldID=null");
+ }
+ }
+ else
+ {
+ LOGD("## getInboundGroupSessionInstanceId() ERROR! GetObjectClass=null");
+ }
+ }
+ else
+ {
+ LOGD("## getInboundGroupSessionInstanceId() ERROR! aJniEnv=NULL");
+ }
+
+ return instanceId;
+}
+
+
+/**
+* Read the outbound group session instance ID of the calling object (aJavaObject).<br>
+* @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("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null");
+ }
+ }
+ else
+ {
+ LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null");
+ }
+ }
+ else
+ {
+ LOGD("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL");
+ }
+
+ return instanceId;
+}
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 40af39d..46c0ee4 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
@@ -15,7 +15,6 @@
*/
#include "olm_outbound_group_session.h"
-#include "olm_utility.h"
/**
@@ -31,12 +30,12 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{
- LOGE("## releaseSessionJni(): failure - invalid inbound group session instance");
+ LOGE("## releaseSessionJni(): failure - invalid outbound group session instance");
}
else
{
size_t retCode = olm_clear_outbound_group_session(sessionPtr);
- LOGD("## releaseSessionJni(): clear_inbound_group_session=%lu",retCode);
+ LOGD("## releaseSessionJni(): clear_outbound_group_session=%lu",retCode);
LOGD("## releaseSessionJni(): IN");
free(sessionPtr);
@@ -73,25 +72,26 @@ JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *e
}
/**
- * Create a new outbound session.<br>
+ * Start a new outbound session.<br>
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
*/
-JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz)
+JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz)
{
jint retCode = ERROR_CODE_KO;
OlmOutboundGroupSession *sessionPtr = NULL;
uint8_t *randomBuffPtr = NULL;
- size_t sessionResult;
+
+ LOGD("## initOutboundGroupSessionJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{
- LOGE("## initOutboundGroupSessionJni(): failure - invalid inbound 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);
if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength))
{
LOGE("## initOutboundGroupSessionJni(): failure - random buffer init");
@@ -103,190 +103,224 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(J
LOGW("## initOutboundGroupSessionJni(): random buffer is not required");
}
- size_t sessionResult = olm_init_outbound_group_session(sessionPtr, sessionKeyPtr, sessionKeyLength);
+ size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength);
if(sessionResult == olm_error()) {
- const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
- LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr);
+ const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
+ LOGE("## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",errorMsgPtr);
}
else
{
retCode = ERROR_CODE_OK;
- LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult);
+ LOGD("## initOutboundGroupSessionJni(): success - result=%lu", sessionResult);
}
-
-
}
}
-
- else if(0 == aSessionKey)
+ if(NULL != randomBuffPtr)
{
- LOGE("## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey");
+ free(randomBuffPtr);
}
- else if(NULL == (sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0)))
+
+ return retCode;
+}
+
+/**
+* Get a base64-encoded identifier for this outbound group session.
+*/
+JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
+{
+ OlmOutboundGroupSession *sessionPtr = NULL;
+ uint8_t *sessionIdPtr = NULL;
+ jstring returnValueStr=0;
+
+ LOGD("## sessionIdentifierJni(): outbound group session IN");
+
+ if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{
- LOGE("## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM");
+ LOGE("## sessionIdentifierJni(): failure - invalid outbound group session instance");
}
else
{
- size_t sessionKeyLength = (size_t)env->GetStringUTFLength(aSessionKey);
- LOGD("## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu",sessionKeyLength);
+ // get the size to alloc
+ size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr);
+ LOGD("## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",lengthSessionId);
- 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);
+ if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t))))
+ {
+ LOGE("## sessionIdentifierJni(): failure - outbound identifier allocation OOM");
}
else
{
- retCode = ERROR_CODE_OK;
- LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult);
- }
- }
+ size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
+ 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);
+ }
+ else
+ {
+ // update length
+ sessionIdPtr[result] = static_cast<char>('\0');
+ LOGD("## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",result, (char*)sessionIdPtr);
+ returnValueStr = env->NewStringUTF((const char*)sessionIdPtr);
+ }
- // free local alloc
- if(NULL!= sessionKeyPtr)
- {
- env->ReleaseStringUTFChars(aSessionKey, (const char*)sessionKeyPtr);
- }
+ // free alloc
+ free(sessionIdPtr);
+ }
+ }
- return retCode;
+ return returnValueStr;
}
-JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
+/**
+* Get the current message index for this session.<br>
+* Each message is sent with an increasing index, this
+* method returns the index for the next message.
+* @return current session index
+*/
+JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz)
{
- OlmInboundGroupSession *sessionPtr = NULL;
- uint8_t *sessionIdPtr = NULL;
- jstring returnValueStr=0;
+ OlmOutboundGroupSession *sessionPtr = NULL;
+ jint indexRetValue = 0;
- // get the size to alloc to contain the id
- size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr);
+ LOGD("## messageIndexJni(): IN");
- if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
+ if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
+ {
+ LOGE("## messageIndexJni(): failure - invalid outbound group session instance");
+ }
+ else
{
- LOGE("## sessionIdentifierJni(): failure - invalid inbound group session instance");
+ indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr));
}
- else if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t))))
+ LOGD("## messageIndexJni(): success - index=%d",indexRetValue);
+
+ return indexRetValue;
+}
+
+
+/**
+* Get the base64-encoded current ratchet key for this session.<br>
+*/
+JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz)
+{
+ OlmOutboundGroupSession *sessionPtr = NULL;
+ uint8_t *sessionKeyPtr = NULL;
+ jstring returnValueStr=0;
+
+ LOGD("## sessionKeyJni(): outbound group session IN");
+
+ if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{
- LOGE("## sessionIdentifierJni(): failure - identifier allocation OOM");
+ LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance");
}
else
{
- size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
- if (result == olm_error())
+ // get the size to alloc
+ size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr);
+ LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",sessionKeyLength);
+
+ if(NULL == (sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t))))
{
- const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
- LOGE("## sessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr);
+ LOGE(" ## sessionKeyJni(): failure - session key allocation OOM");
}
else
{
- // update length
- sessionIdPtr[result] = static_cast<char>('\0');
+ size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength);
+ if (result == olm_error())
+ {
+ const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
+ LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",errorMsgPtr);
+ }
+ else
+ {
+ // update length
+ sessionKeyPtr[result] = static_cast<char>('\0');
+ LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%s",result, (char*)sessionKeyPtr);
+ returnValueStr = env->NewStringUTF((const char*)sessionKeyPtr);
+ }
- LOGD("## sessionIdentifierJni(): success - result=%lu sessionId=%s",result, (char*)sessionIdPtr);
- returnValueStr = env->NewStringUTF((const char*)sessionIdPtr);
+ // free alloc
+ free(sessionKeyPtr);
}
- free(sessionIdPtr);
}
return returnValueStr;
}
-JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg)
+
+JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsg)
{
- jstring decryptedMsgRetValue = 0;
- OlmInboundGroupSession *sessionPtr = NULL;
- const char *encryptedMsgPtr = NULL;
- uint8_t *plainTextMsgPtr = NULL;
- uint8_t *tempEncryptedPtr = NULL;
+ jstring encryptedMsgRetValue = 0;
+ OlmOutboundGroupSession *sessionPtr = NULL;
+ const char *clearMsgPtr = NULL;
+ uint8_t *encryptedMsgPtr = NULL;
- LOGD("## decryptMessageJni(): IN");
+ LOGD("## encryptMessageJni(): IN");
- if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
+ if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{
- LOGE("## decryptMessageJni(): failure - invalid inbound group session ptr=NULL");
+ LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL");
}
- else if(0 == aEncryptedMsg)
+ else if(0 == aClearMsg)
{
- LOGE("## decryptMessageJni(): failure - invalid clear message");
+ LOGE(" ## encryptMessageJni(): failure - invalid clear message");
}
- else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0)))
+ else if(0 == (clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0)))
{
- LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM");
+ LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM");
}
else
{
- // get encrypted message length
- size_t encryptedMsgLength = (size_t)env->GetStringUTFLength(aEncryptedMsg);
+ // get clear message length
+ size_t clearMsgLength = (size_t)env->GetStringUTFLength(aClearMsg);
+ LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",clearMsgLength);
- // create a dedicated temp buffer to be used in next Olm API calls
- if(NULL == (tempEncryptedPtr = static_cast<uint8_t*>(malloc(encryptedMsgLength*sizeof(uint8_t)))))
+ // compute max encrypted length
+ size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength);
+ if(NULL == (encryptedMsgPtr = (uint8_t*)malloc(encryptedMsgLength*sizeof(uint8_t))))
{
- LOGE("## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM");
+ LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM");
}
else
{
- memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
- LOGD("## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgLength,encryptedMsgPtr);
-
- // get max plaintext length
- size_t maxPlainTextLength = olm_group_decrypt_max_plaintext_length(sessionPtr,
- tempEncryptedPtr,
- encryptedMsgLength);
- if(maxPlainTextLength == olm_error())
+ LOGD(" ## encryptMessageJni(): estimated encryptedMsgLength=%lu",encryptedMsgLength);
+
+ size_t decryptedLength = olm_group_encrypt(sessionPtr,
+ (uint8_t*)clearMsgPtr,
+ clearMsgLength,
+ encryptedMsgPtr,
+ encryptedMsgLength);
+ if(decryptedLength == olm_error())
{
- const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
- LOGE("## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr);
+ const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
+ LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr);
}
else
{
- LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",maxPlainTextLength);
-
- // allocate output decrypted message
- plainTextMsgPtr = static_cast<uint8_t*>(malloc(maxPlainTextLength*sizeof(uint8_t)));
-
- // decrypt, but before reload encrypted buffer (previous one was destroyed)
- memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
- size_t plaintextLength = olm_group_decrypt(sessionPtr,
- tempEncryptedPtr,
- encryptedMsgLength,
- plainTextMsgPtr,
- maxPlainTextLength);
- if(plaintextLength == olm_error())
- {
- const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
- LOGE("## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr);
- }
- else
- {
- // update decrypted buffer size
- plainTextMsgPtr[plaintextLength] = static_cast<char>('\0');
-
- LOGD("## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, (char*)plainTextMsgPtr);
- decryptedMsgRetValue = env->NewStringUTF((const char*)plainTextMsgPtr);
- }
+ // update decrypted buffer size
+ encryptedMsgPtr[decryptedLength] = static_cast<char>('\0');
+
+ LOGD(" ## encryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",decryptedLength, (char*)encryptedMsgPtr);
+ encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr);
}
}
- }
+ }
// free alloc
- if(NULL != encryptedMsgPtr)
+ if(NULL != clearMsgPtr)
{
- env->ReleaseStringUTFChars(aEncryptedMsg, encryptedMsgPtr);
+ env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr);
}
- if(NULL != tempEncryptedPtr)
- {
- free(tempEncryptedPtr);
- }
-
- if(NULL != plainTextMsgPtr)
+ if(NULL != encryptedMsgPtr)
{
- free(plainTextMsgPtr);
+ free(encryptedMsgPtr);
}
- return decryptedMsgRetValue;
+ return encryptedMsgRetValue;
}
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 1270dc2..6e264aa 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
@@ -31,9 +31,12 @@ extern "C" {
JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz);
-JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz, jstring aSessionKey);
+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 jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg);
+JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz);
+JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz);
+
+JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsgPtr);
#ifdef __cplusplus
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 435540c..5911591 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
@@ -15,7 +15,6 @@
*/
#include "olm_session.h"
-#include "olm_utility.h"
/**
@@ -120,6 +119,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
else
{ // allocate random buffer
size_t randomSize = olm_create_outbound_session_random_length(sessionPtr);
+ LOGD("## initOutboundSessionJni(): randomSize=%lu",randomSize);
if((0!=randomSize) && !setRandomInBuffer(&randomBuffPtr, randomSize))
{
LOGE("## initOutboundSessionJni(): failure - random buffer init");
@@ -485,7 +485,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz
// 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);
-
+ LOGD("## encryptMessageJni(): randomLength=%lu", randomLength);
if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength))
{
LOGE("## encryptMessageJni(): failure - random buffer init");