2016-10-13 19:21:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 OpenMarket Ltd
|
2016-12-22 11:12:41 +01:00
|
|
|
* Copyright 2016 Vector Creations Ltd
|
2016-10-13 19:21:01 +02:00
|
|
|
*
|
|
|
|
* 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_outbound_group_session.h"
|
|
|
|
|
2016-10-18 15:59:36 +02:00
|
|
|
using namespace AndroidOlmSdk;
|
2016-10-13 19:21:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the session allocation made by initializeOutboundGroupSessionMemory().<br>
|
|
|
|
* This method MUST be called when java counter part account instance is done.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz)
|
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGD("## releaseSessionJni(): OutBound group session IN");
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
OlmOutboundGroupSession* sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!sessionPtr)
|
|
|
|
{
|
|
|
|
LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr);
|
2016-10-14 18:43:57 +02:00
|
|
|
|
2016-10-27 18:02:38 +02:00
|
|
|
#ifdef ENABLE_JNI_LOG
|
2017-01-03 09:32:59 +01:00
|
|
|
size_t retCode = olm_clear_outbound_group_session(sessionPtr);
|
|
|
|
LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",static_cast<long unsigned int>(retCode));
|
2016-10-27 18:02:38 +02:00
|
|
|
#else
|
2017-01-03 09:32:59 +01:00
|
|
|
olm_clear_outbound_group_session(sessionPtr);
|
2016-10-27 18:02:38 +02:00
|
|
|
#endif
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGD(" ## releaseSessionJni(): free IN");
|
|
|
|
free(sessionPtr);
|
|
|
|
LOGD(" ## releaseSessionJni(): free OUT");
|
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Initialize a new outbound group session and return it to JAVA side.<br>
|
|
|
|
* Since a C prt is returned as a jlong, special care will be taken
|
|
|
|
* to make the cast (OlmOutboundGroupSession* => jlong) platform independent.
|
|
|
|
* @return the initialized OlmOutboundGroupSession* instance or throw an exception
|
|
|
|
**/
|
2016-10-23 23:55:45 +02:00
|
|
|
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
const char* errorMessage = NULL;
|
|
|
|
|
2016-10-13 19:21:01 +02:00
|
|
|
OlmOutboundGroupSession* sessionPtr = NULL;
|
2016-10-14 18:43:57 +02:00
|
|
|
size_t sessionSize = 0;
|
|
|
|
|
2016-10-23 23:55:45 +02:00
|
|
|
LOGD("## createNewSessionJni(): outbound group session IN");
|
2016-10-14 18:43:57 +02:00
|
|
|
sessionSize = olm_outbound_group_session_size();
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (0 == sessionSize)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-23 23:55:45 +02:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - outbound group session size = 0");
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = "outbound group session size = 0";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2017-01-10 11:40:57 +01:00
|
|
|
else if (!(sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize)))
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - outbound group session OOM");
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = "outbound group session OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
sessionPtr = olm_outbound_group_session(sessionPtr);
|
|
|
|
LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",static_cast<long unsigned int>(sessionSize));
|
|
|
|
|
2016-10-13 19:21:01 +02:00
|
|
|
// compute random buffer
|
|
|
|
size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr);
|
2017-01-03 09:32:59 +01:00
|
|
|
uint8_t *randomBuffPtr = NULL;
|
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGW(" ## createNewSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength))
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - random buffer init");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "random buffer init";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
if (0 == randomLength)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGW(" ## createNewSessionJni(): random buffer is not required");
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (sessionResult == olm_error()) {
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr);
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - init outbound session creation Msg=%s", errorMessage);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD(" ## createNewSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-09 15:12:15 +01:00
|
|
|
// clear the random buffer
|
|
|
|
memset(randomBuffPtr, 0, randomLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
free(randomBuffPtr);
|
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2016-10-14 15:27:20 +02:00
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
if (sessionPtr)
|
|
|
|
{
|
|
|
|
olm_clear_outbound_group_session(sessionPtr);
|
|
|
|
free(sessionPtr);
|
|
|
|
}
|
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
2017-01-10 11:40:57 +01:00
|
|
|
|
|
|
|
return (jlong)(intptr_t)sessionPtr;
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Return the session identifier.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @return the session identifier
|
|
|
|
*/
|
2017-01-03 16:12:20 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
|
2016-10-14 15:27:20 +02:00
|
|
|
{
|
|
|
|
LOGD("## sessionIdentifierJni(): outbound group session IN");
|
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-03 16:12:20 +01:00
|
|
|
OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
|
|
|
|
jbyteArray returnValue = 0;
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "invalid outbound group session instance";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
// get the size to alloc
|
|
|
|
size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr);
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-09 15:03:15 +01:00
|
|
|
uint8_t *sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (!sessionIdPtr)
|
2016-10-14 15:27:20 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "outbound identifier allocation OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
if (result == olm_error())
|
|
|
|
{
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = reinterpret_cast<const char*>(olm_outbound_group_session_last_error(sessionPtr));
|
|
|
|
LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s", errorMessage);
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 16:12:20 +01:00
|
|
|
returnValue = env->NewByteArray(result);
|
|
|
|
env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr);
|
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId= %.*s",static_cast<long unsigned int>(result), static_cast<int>(result), reinterpret_cast<char*>(sessionIdPtr));
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
// free alloc
|
|
|
|
free(sessionIdPtr);
|
|
|
|
}
|
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
return returnValue;
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* 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.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @return current session index
|
|
|
|
*/
|
2016-10-14 15:27:20 +02:00
|
|
|
JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
OlmOutboundGroupSession *sessionPtr = NULL;
|
|
|
|
jint indexRetValue = 0;
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
LOGD("## messageIndexJni(): IN");
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
|
2016-10-14 15:27:20 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance");
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
|
|
|
else
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr));
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue);
|
2016-10-14 15:27:20 +02:00
|
|
|
|
|
|
|
return indexRetValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Return the session key.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @return the session key
|
|
|
|
*/
|
2017-01-03 16:12:20 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz)
|
2016-10-14 15:27:20 +02:00
|
|
|
{
|
|
|
|
LOGD("## sessionKeyJni(): outbound group session IN");
|
2017-01-03 16:12:20 +01:00
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-03 09:32:59 +01:00
|
|
|
OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz);
|
2017-01-03 16:12:20 +01:00
|
|
|
jbyteArray returnValue = 0;
|
2016-10-14 15:27:20 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "invalid outbound group session instance";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
// get the size to alloc
|
|
|
|
size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr);
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength));
|
2016-10-14 15:27:20 +02:00
|
|
|
|
2017-01-09 15:03:15 +01:00
|
|
|
uint8_t *sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (!sessionKeyPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
LOGE(" ## sessionKeyJni(): failure - session key allocation OOM");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "session key allocation OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
if (result == olm_error())
|
|
|
|
{
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s", errorMessage);
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%.*s",static_cast<long unsigned int>(result), static_cast<int>(result), reinterpret_cast<char*>(sessionKeyPtr));
|
2017-01-03 16:12:20 +01:00
|
|
|
|
|
|
|
returnValue = env->NewByteArray(result);
|
|
|
|
env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionKeyPtr);
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
// free alloc
|
|
|
|
free(sessionKeyPtr);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
return returnValue;
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
/**
|
|
|
|
* Encrypt a bytes buffer messages.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @param aClearMsgBuffer the message to encode
|
|
|
|
* @return the encoded message
|
|
|
|
*/
|
2017-01-04 13:13:13 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGD("## encryptMessageJni(): IN");
|
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-03 16:12:20 +01:00
|
|
|
jbyteArray encryptedMsgRet = 0;
|
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
OlmOutboundGroupSession *sessionPtr = NULL;
|
2017-01-03 11:38:43 +01:00
|
|
|
jbyte* clearMsgPtr = NULL;
|
2018-10-10 21:06:58 +02:00
|
|
|
jboolean clearMsgIsCopied = JNI_FALSE;
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "invalid outbound group session ptr=NULL";
|
2017-01-03 16:41:04 +01:00
|
|
|
}
|
2017-01-03 11:38:43 +01:00
|
|
|
else if (!aClearMsgBuffer)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
LOGE(" ## encryptMessageJni(): failure - invalid clear message");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "invalid clear message";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2018-10-10 21:06:58 +02:00
|
|
|
else if (!(clearMsgPtr = env->GetByteArrayElements(aClearMsgBuffer, &clearMsgIsCopied)))
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "clear message JNI allocation OOM";
|
2017-01-03 16:41:04 +01:00
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
else
|
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
// get clear message length
|
2017-01-03 11:38:43 +01:00
|
|
|
size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer);
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",static_cast<long unsigned int>(clearMsgLength));
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
// compute max encrypted length
|
|
|
|
size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength);
|
2017-01-09 15:03:15 +01:00
|
|
|
uint8_t *encryptedMsgPtr = (uint8_t*)malloc(encryptedMsgLength*sizeof(uint8_t));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (!encryptedMsgPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "encryptedMsgPtr buffer OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD(" ## encryptMessageJni(): estimated encryptedMsgLength=%lu",static_cast<long unsigned int>(encryptedMsgLength));
|
2016-10-14 15:27:20 +02:00
|
|
|
|
2016-10-14 18:43:57 +02:00
|
|
|
size_t encryptedLength = olm_group_encrypt(sessionPtr,
|
2016-10-14 15:27:20 +02:00
|
|
|
(uint8_t*)clearMsgPtr,
|
|
|
|
clearMsgLength,
|
|
|
|
encryptedMsgPtr,
|
|
|
|
encryptedMsgLength);
|
2017-01-03 16:41:04 +01:00
|
|
|
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (encryptedLength == olm_error())
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = olm_outbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s", errorMessage);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%.*s",static_cast<long unsigned int>(encryptedLength), static_cast<int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr));
|
2017-01-03 16:12:20 +01:00
|
|
|
|
|
|
|
encryptedMsgRet = env->NewByteArray(encryptedLength);
|
|
|
|
env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
free(encryptedMsgPtr);
|
|
|
|
}
|
2017-01-03 16:41:04 +01:00
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|
|
|
|
// free alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (clearMsgPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2018-10-10 21:06:58 +02:00
|
|
|
if (clearMsgIsCopied)
|
|
|
|
{
|
|
|
|
memset(clearMsgPtr, 0, (size_t)env->GetArrayLength(aClearMsgBuffer));
|
|
|
|
}
|
2017-01-03 11:38:43 +01:00
|
|
|
env->ReleaseByteArrayElements(aClearMsgBuffer, clearMsgPtr, JNI_ABORT);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
return encryptedMsgRet;
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2016-10-21 19:05:20 +02:00
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Serialize and encrypt session instance into a base64 string.<br>
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @param aKey key used to encrypt the serialized session data
|
|
|
|
* @return a base64 string if operation succeed, null otherwise
|
|
|
|
**/
|
2017-01-04 18:30:35 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 13:13:13 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-04 18:30:35 +01:00
|
|
|
jbyteArray returnValue = 0;
|
2017-01-04 13:13:13 +01:00
|
|
|
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte* keyPtr = NULL;
|
2018-10-17 21:50:36 +02:00
|
|
|
jboolean keyWasCopied = JNI_FALSE;
|
2016-10-21 19:05:20 +02:00
|
|
|
OlmOutboundGroupSession* sessionPtr = NULL;
|
|
|
|
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD("## outbound group session serializeJni(): IN");
|
2016-10-21 19:05:20 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - invalid session ptr");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "invalid session ptr";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aKeyBuffer)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - invalid key");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "invalid key";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
2018-10-17 21:50:36 +02:00
|
|
|
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied)))
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "keyPtr JNI allocation OOM";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t pickledLength = olm_pickle_outbound_group_session_length(sessionPtr);
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
|
2016-10-21 19:05:20 +02:00
|
|
|
|
2017-01-09 15:03:15 +01:00
|
|
|
void *pickledPtr = malloc(pickledLength*sizeof(uint8_t));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if(!pickledPtr)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM");
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = "pickledPtr buffer OOM";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t result = olm_pickle_outbound_group_session(sessionPtr,
|
|
|
|
(void const *)keyPtr,
|
|
|
|
keyLength,
|
|
|
|
(void*)pickledPtr,
|
|
|
|
pickledLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (result == olm_error())
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 13:13:13 +01:00
|
|
|
errorMessage = olm_outbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## serializeJni(): failure - olm_pickle_outbound_group_session() Msg=%s", errorMessage);
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(result), static_cast<char*>(pickledPtr));
|
2017-01-04 18:30:35 +01:00
|
|
|
|
|
|
|
returnValue = env->NewByteArray(pickledLength);
|
|
|
|
env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr);
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
free(pickledPtr);
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
// free alloc
|
|
|
|
if (keyPtr)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2018-10-17 21:50:36 +02:00
|
|
|
if (keyWasCopied) {
|
|
|
|
memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer));
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 13:13:13 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-04 18:30:35 +01:00
|
|
|
return returnValue;
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
/**
|
|
|
|
* Allocate a new session and initialize it with the serialisation data.<br>
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @param aSerializedData the session serialisation buffer
|
|
|
|
* @param aKey the key used to encrypt the serialized account data
|
|
|
|
* @return the deserialized session
|
|
|
|
**/
|
|
|
|
JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
const char* errorMessage = NULL;
|
|
|
|
size_t sessionSize = olm_outbound_group_session_size();
|
2016-10-21 19:05:20 +02:00
|
|
|
OlmOutboundGroupSession* sessionPtr = NULL;
|
2017-01-10 11:40:57 +01:00
|
|
|
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte* keyPtr = NULL;
|
2018-10-17 21:50:36 +02:00
|
|
|
jboolean keyWasCopied = JNI_FALSE;
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte* pickledPtr = NULL;
|
2016-10-21 19:05:20 +02:00
|
|
|
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD("## deserializeJni(): IN");
|
2016-10-21 19:05:20 +02:00
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
if (!sessionSize)
|
|
|
|
{
|
|
|
|
LOGE(" ## deserializeJni(): failure - outbound group session size = 0");
|
|
|
|
errorMessage = "outbound group session size = 0";
|
|
|
|
}
|
|
|
|
else if (!(sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize)))
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## deserializeJni(): failure - session failure OOM");
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = "session failure OOM";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aKeyBuffer)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## deserializeJni(): failure - invalid key");
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = "invalid key";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aSerializedDataBuffer)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## deserializeJni(): failure - serialized data");
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = "invalid serialized data";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
2018-10-17 21:50:36 +02:00
|
|
|
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied)))
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM");
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = "keyPtr JNI allocation OOM";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0)))
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM");
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = "pickledPtr JNI allocation OOM";
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
sessionPtr = olm_outbound_group_session(sessionPtr);
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer);
|
|
|
|
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
|
2017-01-10 14:45:20 +01:00
|
|
|
LOGD(" ## deserializeJni(): pickled=%.*s", static_cast<int>(pickledLength), (char const *)pickledPtr);
|
2016-10-21 19:05:20 +02:00
|
|
|
|
|
|
|
size_t result = olm_unpickle_outbound_group_session(sessionPtr,
|
|
|
|
(void const *)keyPtr,
|
|
|
|
keyLength,
|
|
|
|
(void*)pickledPtr,
|
|
|
|
pickledLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (result == olm_error())
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = olm_outbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## deserializeJni(): failure - olm_unpickle_outbound_group_session() Msg=%s", errorMessage);
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result));
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (keyPtr)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2018-10-17 21:50:36 +02:00
|
|
|
if (keyWasCopied) {
|
|
|
|
memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer));
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (pickledPtr)
|
2016-10-21 19:05:20 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT);
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
if (sessionPtr)
|
|
|
|
{
|
|
|
|
olm_clear_outbound_group_session(sessionPtr);
|
|
|
|
free(sessionPtr);
|
|
|
|
}
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (jlong)(intptr_t)sessionPtr;
|
2016-10-21 19:05:20 +02:00
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|