- Add inbound and outbound group sessions

- Modify constructors for inbound and outbound group sessions
- Add new Ecxception class
This commit is contained in:
pedroGitt 2016-10-14 15:27:20 +02:00
parent 57ec6fff88
commit 1028099550
11 changed files with 560 additions and 200 deletions

View file

@ -34,19 +34,33 @@ public class OlmInboundGroupSession implements Serializable {
*/ */
private long mNativeOlmInboundGroupSessionId; private long mNativeOlmInboundGroupSessionId;
public OlmInboundGroupSession() {
initNewSession();
}
/** /**
* Getter on the native inbound group session ID. * Getter on the native inbound group session ID.
* @return native inbound group session ID * @return native inbound group session ID
*/ */
public long getOlmInboundGroupSessionId(){ public long getOlmInboundGroupSessionId() {
return mNativeOlmInboundGroupSessionId; return mNativeOlmInboundGroupSessionId;
} }
/**
* Constructor.<br>
* Create and save a new native session instance ID and start a new inbound group session.
* The session key parameter is retrieved from a outbound group session
* See {@link #initNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)}
* @param aSessionKey session key
* @throws OlmUtilsException
*/
public OlmInboundGroupSession(String aSessionKey) throws OlmUtilsException {
if(initNewSession()) {
if( 0 != initInboundGroupSessionWithSessionKey(aSessionKey)) {
releaseSession();// prevent memory leak before throwing
throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION);
}
} else {
throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE);
}
}
/** /**
* Release native session and invalid its JAVA reference counter part.<br> * Release native session and invalid its JAVA reference counter part.<br>
* Public API for {@link #releaseSessionJni()}. * Public API for {@link #releaseSessionJni()}.
@ -88,12 +102,13 @@ public class OlmInboundGroupSession implements Serializable {
private native long initNewSessionJni(); private native long initNewSessionJni();
/** /**
* Creates a new inbound group session.<br> * Start a new inbound group session.<br>
* The session key parameter is retrieved from a outbound group session. * The session key parameter is retrieved from a outbound group session
* see {@link OlmOutboundGroupSession#sessionKey()}
* @param aSessionKey session key * @param aSessionKey session key
* @return 0 if operation succeed, -1 otherwise * @return 0 if operation succeed, -1 otherwise
*/ */
public int initInboundGroupSessionWithSessionKey(String aSessionKey) { private int initInboundGroupSessionWithSessionKey(String aSessionKey) {
int retCode = -1; int retCode = -1;
if(TextUtils.isEmpty(aSessionKey)){ if(TextUtils.isEmpty(aSessionKey)){

View file

@ -28,16 +28,30 @@ public class OlmOutboundGroupSession {
*/ */
private long mNativeOlmOutboundGroupSessionId; private long mNativeOlmOutboundGroupSessionId;
public OlmOutboundGroupSession() {
initNewSession();
}
/** /**
* Getter on the native outbound group session ID. * Getter on the native outbound group session ID.
* @return native outbound group session ID * @return native outbound group session ID
*/ */
public long getOlmInboundGroupSessionId(){ public long getOlmInboundGroupSessionId(){
return mNativeOlmInboundGroupSessionId; return mNativeOlmOutboundGroupSessionId;
}
/**
* Constructor.<br>
* Create and save a new session native instance ID and
* initialise a new outbound group session.<br>
* See {@link #initNewSession()} and {@link #initOutboundGroupSession()}
* @throws OlmUtilsException
*/
public OlmOutboundGroupSession() throws OlmUtilsException {
if(initNewSession()) {
if( 0 != initOutboundGroupSession()) {
releaseSession();// prevent memory leak before throwing
throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION);
}
} else {
throw new OlmUtilsException(OlmUtilsException.EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE);
}
} }
/** /**
@ -45,9 +59,8 @@ public class OlmOutboundGroupSession {
* Public API for {@link #releaseSessionJni()}. * Public API for {@link #releaseSessionJni()}.
* To be called before any other API call. * To be called before any other API call.
*/ */
public void releaseSession(){ public void releaseSession() {
releaseSessionJni(); releaseSessionJni();
mNativeOlmOutboundGroupSessionId = 0; mNativeOlmOutboundGroupSessionId = 0;
} }
@ -80,54 +93,66 @@ public class OlmOutboundGroupSession {
*/ */
private native long initNewSessionJni(); private native long initNewSessionJni();
/** /**
* Creates a new outbound group session.<br> * Start a new outbound group session.<br>
* The session key parameter is retrieved from a outbound group session.
* @return 0 if operation succeed, -1 otherwise * @return 0 if operation succeed, -1 otherwise
*/ */
public int initOutboundGroupSession() { private int initOutboundGroupSession() {
return initOutboundGroupSessionJni(); return initOutboundGroupSessionJni();
} }
public native int initOutboundGroupSessionJni(); private native int initOutboundGroupSessionJni();
/**
* Get a base64-encoded identifier for this session.
* @return session identifier if operation succeed, null otherwise.
*/
public String sessionIdentifier() { public String sessionIdentifier() {
String retValue = null; String retValue = null;
//retValue = sessionIdentifierJni(); retValue = sessionIdentifierJni();
return retValue; return retValue;
} }
public native String sessionIdentifierJni(); private native String sessionIdentifierJni();
/**
* Get the current message index for this session.<br>
* Each message is sent with an increasing index, this
public long messageIndex() { * method returns the index for the next message.
long retValue =0; * @return current session index
//retValue = messageIndexJni(); */
public int messageIndex() {
int retValue =0;
retValue = messageIndexJni();
return retValue; return retValue;
} }
private native long messageIndexJni(); private native int messageIndexJni();
/**
* Get the base64-encoded current ratchet key for this session.<br>
* Each message is sent with a different ratchet key. This method returns the
* ratchet key that will be used for the next message.
* @return outbound session key
*/
public String sessionKey() { public String sessionKey() {
String retValue = null; String retValue = null;
//retValue = sessionKeyJni(); retValue = sessionKeyJni();
return retValue; return retValue;
} }
private native String sessionKeyJni(); private native String sessionKeyJni();
/**
* Encrypt some plain-text message.<br>
* @param aClearMsg message to be encrypted
* @return the encrypted message if operation succeed, null otherwise
*/
public String encryptMessage(String aClearMsg) { public String encryptMessage(String aClearMsg) {
String retValue = null; String retValue = null;
//retValue = encryptMessageJni(aClearMsg);
if(!TextUtils.isEmpty(aClearMsg)) {
retValue = encryptMessageJni(aClearMsg);
}
return retValue; return retValue;
} }

View file

@ -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 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;
}
}

View file

@ -45,8 +45,9 @@ $(SRC_ROOT_DIR)/lib/crypto-algorithms/aes.c \
$(SRC_ROOT_DIR)/lib/curve25519-donna/curve25519-donna.c \ $(SRC_ROOT_DIR)/lib/curve25519-donna/curve25519-donna.c \
olm_account.cpp \ olm_account.cpp \
olm_session.cpp \ olm_session.cpp \
olm_utility.cpp \ olm_jni_helper.cpp \
olm_inbound_group_session.cpp olm_inbound_group_session.cpp \
olm_outbound_group_session.cpp
LOCAL_LDLIBS := -llog LOCAL_LDLIBS := -llog

View file

@ -15,7 +15,6 @@
*/ */
#include "olm_account.h" #include "olm_account.h"
#include "olm_utility.h"
/** /**
* Init memory allocation for account creation. * Init memory allocation for account creation.
@ -85,9 +84,12 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
} }
else else
{ {
// allocate random buffer // get random buffer size
randomSize = olm_create_account_random_length(accountPtr); 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"); 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); randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys);
LOGD("## generateOneTimeKeys(): randomLength=%ld", randomLength); LOGD("## generateOneTimeKeys(): randomLength=%ld", randomLength);
if(!setRandomInBuffer(&randomBufferPtr, randomLength)) if((0!=randomLength) && !setRandomInBuffer(&randomBufferPtr, randomLength))
{ {
LOGE("## generateOneTimeKeys(): failure - random buffer init"); LOGE("## generateOneTimeKeys(): failure - random buffer init");
} }

View file

@ -15,7 +15,6 @@
*/ */
#include "olm_inbound_group_session.h" #include "olm_inbound_group_session.h"
#include "olm_utility.h"
/** /**
@ -123,22 +122,30 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes
} }
/**
* Get a base64-encoded identifier for this inbound group session.
*/
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
{ {
OlmInboundGroupSession *sessionPtr = NULL; OlmInboundGroupSession *sessionPtr = NULL;
uint8_t *sessionIdPtr = NULL; uint8_t *sessionIdPtr = NULL;
jstring returnValueStr=0; jstring returnValueStr=0;
// get the size to alloc to contain the id LOGD("## sessionIdentifierJni(): inbound group session IN");
size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr);
if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE("## sessionIdentifierJni(): failure - invalid inbound group session instance"); LOGE("## sessionIdentifierJni(): failure - invalid inbound group session instance");
} }
else if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)))) else
{ {
LOGE("## sessionIdentifierJni(): failure - identifier allocation OOM"); // 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))))
{
LOGE("## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM");
} }
else else
{ {
@ -146,22 +153,23 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
LOGE("## sessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr); LOGE("## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",errorMsgPtr);
} }
else else
{ {
// update length // update length
sessionIdPtr[result] = static_cast<char>('\0'); sessionIdPtr[result] = static_cast<char>('\0');
LOGD("## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s",result, (char*)sessionIdPtr);
LOGD("## sessionIdentifierJni(): success - result=%lu sessionId=%s",result, (char*)sessionIdPtr);
returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); returnValueStr = env->NewStringUTF((const char*)sessionIdPtr);
} }
free(sessionIdPtr); free(sessionIdPtr);
} }
}
return returnValueStr; return returnValueStr;
} }
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg) JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg)
{ {
jstring decryptedMsgRetValue = 0; jstring decryptedMsgRetValue = 0;
@ -178,7 +186,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
} }
else if(0 == aEncryptedMsg) else if(0 == aEncryptedMsg)
{ {
LOGE("## decryptMessageJni(): failure - invalid clear message"); LOGE("## decryptMessageJni(): failure - invalid encrypted message");
} }
else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0))) else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0)))
{ {

View file

@ -53,4 +53,21 @@ static const int ERROR_CODE_KO = -1;
// constants // constants
static const int ACCOUNT_CREATION_RANDOM_MODULO = 256; 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 #endif

View file

@ -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;
}

View file

@ -15,7 +15,6 @@
*/ */
#include "olm_outbound_group_session.h" #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))) if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE("## releaseSessionJni(): failure - invalid inbound group session instance"); LOGE("## releaseSessionJni(): failure - invalid outbound group session instance");
} }
else else
{ {
size_t retCode = olm_clear_outbound_group_session(sessionPtr); 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"); LOGD("## releaseSessionJni(): IN");
free(sessionPtr); 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 * @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; jint retCode = ERROR_CODE_KO;
OlmOutboundGroupSession *sessionPtr = NULL; OlmOutboundGroupSession *sessionPtr = NULL;
uint8_t *randomBuffPtr = NULL; uint8_t *randomBuffPtr = NULL;
size_t sessionResult;
LOGD("## initOutboundGroupSessionJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE("## initOutboundGroupSessionJni(): failure - invalid inbound group session instance"); LOGE("## initOutboundGroupSessionJni(): failure - invalid outbound group session instance");
} }
else else
{ {
// compute random buffer // compute random buffer
size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr); size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr);
LOGW("## initOutboundGroupSessionJni(): randomLength=%lu",randomLength);
if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength)) if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength))
{ {
LOGE("## initOutboundGroupSessionJni(): failure - random buffer init"); 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"); 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()) { if(sessionResult == olm_error()) {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); LOGE("## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",errorMsgPtr);
} }
else else
{ {
retCode = ERROR_CODE_OK; retCode = ERROR_CODE_OK;
LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult); LOGD("## initOutboundGroupSessionJni(): success - result=%lu", sessionResult);
} }
} }
} }
if(NULL != randomBuffPtr)
else if(0 == aSessionKey)
{ {
LOGE("## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"); free(randomBuffPtr);
}
else if(NULL == (sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0)))
{
LOGE("## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM");
}
else
{
size_t sessionKeyLength = (size_t)env->GetStringUTFLength(aSessionKey);
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);
}
else
{
retCode = ERROR_CODE_OK;
LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult);
}
}
// free local alloc
if(NULL!= sessionKeyPtr)
{
env->ReleaseStringUTFChars(aSessionKey, (const char*)sessionKeyPtr);
} }
return retCode; return retCode;
} }
/**
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) * Get a base64-encoded identifier for this outbound group session.
*/
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
{ {
OlmInboundGroupSession *sessionPtr = NULL; OlmOutboundGroupSession *sessionPtr = NULL;
uint8_t *sessionIdPtr = NULL; uint8_t *sessionIdPtr = NULL;
jstring returnValueStr=0; jstring returnValueStr=0;
// get the size to alloc to contain the id LOGD("## sessionIdentifierJni(): outbound group session IN");
size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr);
if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE("## sessionIdentifierJni(): failure - invalid inbound group session instance"); LOGE("## sessionIdentifierJni(): failure - invalid outbound group session instance");
}
else if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t))))
{
LOGE("## sessionIdentifierJni(): failure - identifier allocation OOM");
} }
else else
{ {
size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); // get the size to alloc
size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr);
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");
}
else
{
size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
if (result == olm_error()) if (result == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
LOGE("## sessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr); LOGE("## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",errorMsgPtr);
} }
else else
{ {
// update length // update length
sessionIdPtr[result] = static_cast<char>('\0'); sessionIdPtr[result] = static_cast<char>('\0');
LOGD("## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",result, (char*)sessionIdPtr);
LOGD("## sessionIdentifierJni(): success - result=%lu sessionId=%s",result, (char*)sessionIdPtr);
returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); returnValueStr = env->NewStringUTF((const char*)sessionIdPtr);
} }
// free alloc
free(sessionIdPtr); free(sessionIdPtr);
} }
}
return returnValueStr; return returnValueStr;
} }
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg)
/**
* 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)
{ {
jstring decryptedMsgRetValue = 0; OlmOutboundGroupSession *sessionPtr = NULL;
OlmInboundGroupSession *sessionPtr = NULL; jint indexRetValue = 0;
const char *encryptedMsgPtr = NULL;
uint8_t *plainTextMsgPtr = NULL;
uint8_t *tempEncryptedPtr = NULL;
LOGD("## decryptMessageJni(): IN"); LOGD("## messageIndexJni(): 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("## messageIndexJni(): failure - invalid outbound group session instance");
}
else if(0 == aEncryptedMsg)
{
LOGE("## decryptMessageJni(): failure - invalid clear message");
}
else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0)))
{
LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM");
} }
else else
{ {
// get encrypted message length indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr));
size_t encryptedMsgLength = (size_t)env->GetStringUTFLength(aEncryptedMsg); }
LOGD("## messageIndexJni(): success - index=%d",indexRetValue);
// create a dedicated temp buffer to be used in next Olm API calls return indexRetValue;
if(NULL == (tempEncryptedPtr = static_cast<uint8_t*>(malloc(encryptedMsgLength*sizeof(uint8_t))))) }
/**
* 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("## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"); LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance");
} }
else else
{ {
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); // get the size to alloc
LOGD("## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgLength,encryptedMsgPtr); size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr);
LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",sessionKeyLength);
// get max plaintext length if(NULL == (sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t))))
size_t maxPlainTextLength = olm_group_decrypt_max_plaintext_length(sessionPtr, {
tempEncryptedPtr, LOGE(" ## sessionKeyJni(): failure - session key allocation OOM");
}
else
{
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);
}
// free alloc
free(sessionKeyPtr);
}
}
return returnValueStr;
}
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsg)
{
jstring encryptedMsgRetValue = 0;
OlmOutboundGroupSession *sessionPtr = NULL;
const char *clearMsgPtr = NULL;
uint8_t *encryptedMsgPtr = NULL;
LOGD("## encryptMessageJni(): IN");
if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{
LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL");
}
else if(0 == aClearMsg)
{
LOGE(" ## encryptMessageJni(): failure - invalid clear message");
}
else if(0 == (clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0)))
{
LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM");
}
else
{
// get clear message length
size_t clearMsgLength = (size_t)env->GetStringUTFLength(aClearMsg);
LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",clearMsgLength);
// 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("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM");
}
else
{
LOGD(" ## encryptMessageJni(): estimated encryptedMsgLength=%lu",encryptedMsgLength);
size_t decryptedLength = olm_group_encrypt(sessionPtr,
(uint8_t*)clearMsgPtr,
clearMsgLength,
encryptedMsgPtr,
encryptedMsgLength); encryptedMsgLength);
if(maxPlainTextLength == olm_error()) if(decryptedLength == olm_error())
{ {
const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
LOGE("## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); 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 else
{ {
// update decrypted buffer size // update decrypted buffer size
plainTextMsgPtr[plaintextLength] = static_cast<char>('\0'); encryptedMsgPtr[decryptedLength] = static_cast<char>('\0');
LOGD("## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, (char*)plainTextMsgPtr); LOGD(" ## encryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",decryptedLength, (char*)encryptedMsgPtr);
decryptedMsgRetValue = env->NewStringUTF((const char*)plainTextMsgPtr); encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr);
}
} }
} }
} }
// free alloc // free alloc
if(NULL != clearMsgPtr)
{
env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr);
}
if(NULL != encryptedMsgPtr) if(NULL != encryptedMsgPtr)
{ {
env->ReleaseStringUTFChars(aEncryptedMsg, encryptedMsgPtr); free(encryptedMsgPtr);
} }
if(NULL != tempEncryptedPtr) return encryptedMsgRetValue;
{
free(tempEncryptedPtr);
}
if(NULL != plainTextMsgPtr)
{
free(plainTextMsgPtr);
}
return decryptedMsgRetValue;
} }

View file

@ -31,9 +31,12 @@ extern "C" {
JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); 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 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(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 #ifdef __cplusplus

View file

@ -15,7 +15,6 @@
*/ */
#include "olm_session.h" #include "olm_session.h"
#include "olm_utility.h"
/** /**
@ -120,6 +119,7 @@ JNIEXPORT jint OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject
else else
{ // allocate random buffer { // allocate random buffer
size_t randomSize = olm_create_outbound_session_random_length(sessionPtr); size_t randomSize = olm_create_outbound_session_random_length(sessionPtr);
LOGD("## initOutboundSessionJni(): randomSize=%lu",randomSize);
if((0!=randomSize) && !setRandomInBuffer(&randomBuffPtr, randomSize)) if((0!=randomSize) && !setRandomInBuffer(&randomBuffPtr, randomSize))
{ {
LOGE("## initOutboundSessionJni(): failure - random buffer init"); 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 // Note: olm_encrypt_random_length() can return 0, which means
// it just does not need new random data to encrypt a new message // it just does not need new random data to encrypt a new message
size_t randomLength = olm_encrypt_random_length(sessionPtr); size_t randomLength = olm_encrypt_random_length(sessionPtr);
LOGD("## encryptMessageJni(): randomLength=%lu", randomLength);
if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength)) if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength))
{ {
LOGE("## encryptMessageJni(): failure - random buffer init"); LOGE("## encryptMessageJni(): failure - random buffer init");