2016-10-05 18:25:09 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 OpenMarket Ltd
|
2016-12-22 11:12:41 +01:00
|
|
|
* Copyright 2016 Vector Creations Ltd
|
2016-10-05 18:25:09 +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_session.h"
|
|
|
|
|
2016-10-18 15:59:36 +02:00
|
|
|
using namespace AndroidOlmSdk;
|
2016-10-05 18:25:09 +02:00
|
|
|
|
|
|
|
/**
|
2016-10-07 17:35:27 +02:00
|
|
|
* Init memory allocation for a session creation.<br>
|
|
|
|
* Make sure releaseSessionJni() is called when one is done with the session instance.
|
2016-10-05 18:25:09 +02:00
|
|
|
* @return valid memory allocation, NULL otherwise
|
|
|
|
**/
|
|
|
|
OlmSession* initializeSessionMemory()
|
|
|
|
{
|
|
|
|
size_t sessionSize = olm_session_size();
|
2017-01-03 09:32:59 +01:00
|
|
|
OlmSession* sessionPtr = (OlmSession*)malloc(sessionSize);
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (sessionPtr)
|
|
|
|
{
|
|
|
|
// init session object
|
|
|
|
sessionPtr = olm_session(sessionPtr);
|
|
|
|
LOGD("## initializeSessionMemory(): success - OLM session size=%lu",static_cast<long unsigned int>(sessionSize));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGE("## initializeSessionMemory(): failure - OOM");
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sessionPtr;
|
|
|
|
}
|
|
|
|
|
2016-10-21 18:09:20 +02:00
|
|
|
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
|
|
|
|
{
|
|
|
|
LOGD("## createNewSessionJni(): IN");
|
|
|
|
OlmSession* accountPtr = initializeSessionMemory();
|
|
|
|
|
2017-01-09 13:55:58 +01:00
|
|
|
if (!accountPtr)
|
|
|
|
{
|
|
|
|
LOGE("## initNewAccount(): failure - init session OOM");
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), "init session OOM");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD(" ## createNewSessionJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
|
|
|
|
}
|
|
|
|
|
2016-10-21 18:09:20 +02:00
|
|
|
return (jlong)(intptr_t)accountPtr;
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:53:49 +02:00
|
|
|
JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGD("## releaseSessionJni(): IN");
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession* sessionPtr = getSessionInstanceId(env, thiz);
|
2016-10-24 17:21:28 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!sessionPtr)
|
|
|
|
{
|
|
|
|
LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
olm_clear_session(sessionPtr);
|
2016-10-11 15:53:49 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
// even if free(NULL) does not crash, logs are performed for debug purpose
|
|
|
|
free(sessionPtr);
|
|
|
|
}
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// *********************************************************************
|
|
|
|
// ********************** OUTBOUND SESSION *****************************
|
|
|
|
// *********************************************************************
|
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Create a new in-bound session for sending/receiving messages from an
|
|
|
|
* incoming PRE_KEY message.<br> The recipient is defined as the entity
|
|
|
|
* with whom the session is established.
|
|
|
|
* @param aOlmAccountId account instance
|
|
|
|
* @param aTheirIdentityKey the identity key of the recipient
|
|
|
|
* @param aTheirOneTimeKey the one time key of the recipient or an exception is thrown
|
|
|
|
**/
|
2017-01-04 14:35:08 +01:00
|
|
|
JNIEXPORT void OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKeyBuffer, jbyteArray aTheirOneTimeKeyBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession* sessionPtr = getSessionInstanceId(env, thiz);
|
2017-01-04 14:35:08 +01:00
|
|
|
const char* errorMessage = NULL;
|
2016-10-05 18:25:09 +02:00
|
|
|
OlmAccount* accountPtr = NULL;
|
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initOutboundSessionJni(): failure - invalid Session ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Session ptr=NULL";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(accountPtr = (OlmAccount*)aOlmAccountId))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initOutboundSessionJni(): failure - invalid Account ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Account ptr=NULL";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aTheirIdentityKeyBuffer || !aTheirOneTimeKeyBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initOutboundSessionJni(): failure - invalid keys");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid keys";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
2017-01-03 09:32:59 +01:00
|
|
|
{
|
2016-10-05 18:25:09 +02:00
|
|
|
size_t randomSize = olm_create_outbound_session_random_length(sessionPtr);
|
2017-01-03 09:32:59 +01:00
|
|
|
uint8_t *randomBuffPtr = NULL;
|
|
|
|
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## initOutboundSessionJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if ( (0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2016-10-06 19:55:03 +02:00
|
|
|
LOGE("## initOutboundSessionJni(): failure - random buffer init");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "random buffer init";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
2017-01-03 09:32:59 +01:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte* theirIdentityKeyPtr = NULL;
|
|
|
|
jbyte* theirOneTimeKeyPtr = NULL;
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
// convert identity & one time keys to C strings
|
2017-01-03 14:14:56 +01:00
|
|
|
if (!(theirIdentityKeyPtr = env->GetByteArrayElements(aTheirIdentityKeyBuffer, 0)))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initOutboundSessionJni(): failure - identityKey JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "identityKey JNI allocation OOM";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(theirOneTimeKeyPtr = env->GetByteArrayElements(aTheirOneTimeKeyBuffer, 0)))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initOutboundSessionJni(): failure - one time Key JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "one time Key JNI allocation OOM";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t theirIdentityKeyLength = (size_t)env->GetArrayLength(aTheirIdentityKeyBuffer);
|
|
|
|
size_t theirOneTimeKeyLength = (size_t)env->GetArrayLength(aTheirOneTimeKeyBuffer);
|
2017-01-10 14:45:20 +01:00
|
|
|
LOGD("## initOutboundSessionJni(): identityKey=%.*s oneTimeKey=%.*s", static_cast<int>(theirIdentityKeyLength), theirIdentityKeyPtr, static_cast<int>(theirOneTimeKeyLength), theirOneTimeKeyPtr);
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
size_t sessionResult = olm_create_outbound_session(sessionPtr,
|
2016-10-06 19:55:03 +02:00
|
|
|
accountPtr,
|
|
|
|
theirIdentityKeyPtr,
|
|
|
|
theirIdentityKeyLength,
|
|
|
|
theirOneTimeKeyPtr,
|
|
|
|
theirOneTimeKeyLength,
|
|
|
|
(void*)randomBuffPtr,
|
|
|
|
randomSize);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (sessionResult == olm_error()) {
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = (const char *)olm_session_last_error(sessionPtr);
|
|
|
|
LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s", errorMessage);
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## initOutboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (theirIdentityKeyPtr)
|
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aTheirIdentityKeyBuffer, theirIdentityKeyPtr, JNI_ABORT);
|
2017-01-03 09:32:59 +01:00
|
|
|
}
|
2016-10-06 19:55:03 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (theirOneTimeKeyPtr)
|
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aTheirOneTimeKeyBuffer, theirOneTimeKeyPtr, JNI_ABORT);
|
2017-01-03 09:32:59 +01:00
|
|
|
}
|
2016-10-06 19:55:03 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (randomBuffPtr)
|
|
|
|
{
|
2017-01-10 14:45:20 +01:00
|
|
|
memset(randomBuffPtr, 0, randomSize);
|
2017-01-03 09:32:59 +01:00
|
|
|
free(randomBuffPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-06 19:55:03 +02:00
|
|
|
|
2017-01-04 14:35:08 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// *********************************************************************
|
|
|
|
// *********************** INBOUND SESSION *****************************
|
|
|
|
// *********************************************************************
|
|
|
|
/**
|
|
|
|
* Create a new in-bound session for sending/receiving messages from an
|
|
|
|
* incoming PRE_KEY message.<br>
|
2017-01-10 11:40:57 +01:00
|
|
|
* An exception is thrown if the operation fails.
|
2016-10-05 18:25:09 +02:00
|
|
|
* @param aOlmAccountId account instance
|
2016-10-06 19:55:03 +02:00
|
|
|
* @param aOneTimeKeyMsg PRE_KEY message
|
2016-10-05 18:25:09 +02:00
|
|
|
*/
|
2017-01-04 14:35:08 +01:00
|
|
|
JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession *sessionPtr = getSessionInstanceId(env,thiz);
|
2016-10-05 18:25:09 +02:00
|
|
|
OlmAccount *accountPtr = NULL;
|
|
|
|
size_t sessionResult;
|
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionJni(): failure - invalid Session ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Session ptr=NULL";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(accountPtr = (OlmAccount*)aOlmAccountId))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionJni(): failure - invalid Account ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Account ptr=NULL";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2016-10-11 15:53:49 +02:00
|
|
|
LOGE("## initInboundSessionJni(): failure - invalid message");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid message";
|
2017-01-03 17:20:18 +01:00
|
|
|
}
|
2016-10-05 18:25:09 +02:00
|
|
|
else
|
2017-01-03 09:32:59 +01:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte* messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (!messagePtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionJni(): failure - message JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "message JNI allocation OOM";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer);
|
2017-01-10 14:45:20 +01:00
|
|
|
LOGD("## initInboundSessionJni(): messageLength=%lu message=%.*s", static_cast<long unsigned int>(messageLength), static_cast<int>(messageLength), messagePtr);
|
2016-10-05 18:25:09 +02:00
|
|
|
|
|
|
|
sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (sessionResult == olm_error())
|
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = olm_session_last_error(sessionPtr);
|
|
|
|
LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s", errorMessage);
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## initInboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// free local alloc
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT);
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-04 14:35:08 +01:00
|
|
|
|
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new in-bound session for sending/receiving messages from an
|
|
|
|
* incoming PRE_KEY message based on the recipient identity key.<br>
|
2017-01-10 11:40:57 +01:00
|
|
|
* An exception is thrown if the operation fails.
|
2016-10-05 18:25:09 +02:00
|
|
|
* @param aOlmAccountId account instance
|
|
|
|
* @param aTheirIdentityKey the identity key of the recipient
|
|
|
|
* @param aOneTimeKeyMsg encrypted message
|
|
|
|
*/
|
2017-01-04 14:35:08 +01:00
|
|
|
JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKeyBuffer, jbyteArray aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
const char* errorMessage = NULL;
|
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession *sessionPtr = getSessionInstanceId(env, thiz);
|
2016-10-05 18:25:09 +02:00
|
|
|
OlmAccount *accountPtr = NULL;
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte *messagePtr = NULL;
|
|
|
|
jbyte *theirIdentityKeyPtr = NULL;
|
2016-10-05 18:25:09 +02:00
|
|
|
size_t sessionResult;
|
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Session ptr=NULL";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(accountPtr = (OlmAccount*)aOlmAccountId))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Account ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Account ptr=NULL";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aTheirIdentityKeyBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid theirIdentityKey";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2016-10-11 15:53:49 +02:00
|
|
|
LOGE("## initInboundSessionJni(): failure - invalid one time key message");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid invalid one time key message";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0)))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionFromIdKeyJni(): failure - message JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "message JNI allocation OOM";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if(!(theirIdentityKeyPtr = env->GetByteArrayElements(aTheirIdentityKeyBuffer, 0)))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## initInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "theirIdentityKey JNI allocation OOM";
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer);
|
|
|
|
size_t theirIdentityKeyLength = (size_t)env->GetArrayLength(aTheirIdentityKeyBuffer);
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-10 14:45:20 +01:00
|
|
|
LOGD("## initInboundSessionFromIdKeyJni(): message=%.*s messageLength=%lu", static_cast<int>(messageLength), messagePtr, static_cast<long unsigned int>(messageLength));
|
2016-10-05 18:25:09 +02:00
|
|
|
|
|
|
|
sessionResult = olm_create_inbound_session_from(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, (void*)messagePtr , messageLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (sessionResult == olm_error())
|
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = (const char *)olm_session_last_error(sessionPtr);
|
|
|
|
LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s", errorMessage);
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free local alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (messagePtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT);
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (theirIdentityKeyPtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aTheirIdentityKeyBuffer, theirIdentityKeyPtr, JNI_ABORT);
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 14:35:08 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the PRE_KEY message is for this in-bound session.<br>
|
|
|
|
* This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY).
|
|
|
|
* @param aOneTimeKeyMsg PRE KEY message
|
2017-01-10 11:40:57 +01:00
|
|
|
* @return true if the PRE_KEY message matches
|
2016-10-05 18:25:09 +02:00
|
|
|
*/
|
2017-01-10 11:40:57 +01:00
|
|
|
JNIEXPORT jboolean OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
jboolean retCode = JNI_FALSE;
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession *sessionPtr = getSessionInstanceId(env, thiz);
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte *messagePtr = NULL;
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionJni(): failure - invalid Session ptr=NULL");
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionJni(): failure - invalid one time key message");
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0)))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionJni(): failure - one time key JNI allocation OOM");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer);
|
2016-10-05 18:25:09 +02:00
|
|
|
|
|
|
|
size_t matchResult = olm_matches_inbound_session(sessionPtr, (void*)messagePtr , messageLength);
|
2016-10-25 18:14:39 +02:00
|
|
|
//if(matchResult == olm_error()) {
|
|
|
|
// for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0
|
2017-01-03 09:32:59 +01:00
|
|
|
if (matchResult != 1) {
|
2016-10-23 23:55:45 +02:00
|
|
|
LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
retCode = JNI_TRUE;
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## matchesInboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(matchResult));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:55:03 +02:00
|
|
|
// free local alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (messagePtr)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT);
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 18:25:09 +02:00
|
|
|
return retCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the PRE_KEY message is for this in-bound session based on the sender identity key.<br>
|
|
|
|
* This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY).
|
|
|
|
* @param aTheirIdentityKey the identity key of the sender
|
|
|
|
* @param aOneTimeKeyMsg PRE KEY message
|
2017-01-10 11:40:57 +01:00
|
|
|
* @return true if the PRE_KEY message matches.
|
2016-10-05 18:25:09 +02:00
|
|
|
*/
|
2017-01-10 11:40:57 +01:00
|
|
|
JNIEXPORT jboolean JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aTheirIdentityKeyBuffer, jbyteArray aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
jboolean retCode = JNI_FALSE;
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession *sessionPtr = getSessionInstanceId(env, thiz);
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte *messagePtr = NULL;
|
|
|
|
jbyte *theirIdentityKeyPtr = NULL;
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL");
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aTheirIdentityKeyBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey");
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(theirIdentityKeyPtr = env->GetByteArrayElements(aTheirIdentityKeyBuffer, 0)))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM");
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aOneTimeKeyMsgBuffer)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid one time key message");
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0)))
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
|
|
|
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - one time key JNI allocation OOM");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t identityKeyLength = (size_t)env->GetArrayLength(aTheirIdentityKeyBuffer);
|
|
|
|
size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer);
|
2016-10-05 18:25:09 +02:00
|
|
|
size_t matchResult = olm_matches_inbound_session_from(sessionPtr, (void const *)theirIdentityKeyPtr, identityKeyLength, (void*)messagePtr , messageLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
2016-10-25 18:14:39 +02:00
|
|
|
//if(matchResult == olm_error()) {
|
|
|
|
// for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0
|
2017-01-03 09:32:59 +01:00
|
|
|
if (matchResult != 1)
|
|
|
|
{
|
2016-10-23 23:55:45 +02:00
|
|
|
LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
retCode = JNI_TRUE;
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## matchesInboundSessionFromIdKeyJni(): success - result=%lu", static_cast<long unsigned int>(matchResult));
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:55:03 +02:00
|
|
|
// free local alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (theirIdentityKeyPtr)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aTheirIdentityKeyBuffer, theirIdentityKeyPtr, JNI_ABORT);
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (messagePtr)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT);
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 18:25:09 +02:00
|
|
|
return retCode;
|
|
|
|
}
|
|
|
|
|
2016-10-06 19:55:03 +02:00
|
|
|
/**
|
2016-10-07 17:35:27 +02:00
|
|
|
* Encrypt a message using the session.<br>
|
2017-01-10 11:40:57 +01:00
|
|
|
* An exception is thrown if the operation fails.
|
2016-10-07 17:35:27 +02:00
|
|
|
* @param aClearMsg clear text message
|
|
|
|
* @param [out] aEncryptedMsg ciphered message
|
2017-01-09 15:03:15 +01:00
|
|
|
* @return the encrypted message
|
2016-10-06 19:55:03 +02:00
|
|
|
*/
|
2017-01-09 15:03:15 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aEncryptedMsg)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
2017-01-09 15:03:15 +01:00
|
|
|
jbyteArray encryptedMsgRet = 0;
|
2017-01-04 14:35:08 +01:00
|
|
|
const char* errorMessage = NULL;
|
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession *sessionPtr = getSessionInstanceId(env, thiz);
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte *clearMsgPtr = NULL;
|
2018-10-10 21:06:58 +02:00
|
|
|
jboolean clearMsgIsCopied = JNI_FALSE;
|
2016-10-07 17:35:27 +02:00
|
|
|
jclass encryptedMsgJClass = 0;
|
2016-10-06 19:55:03 +02:00
|
|
|
jfieldID typeMsgFieldId;
|
|
|
|
|
2016-10-12 19:04:50 +02:00
|
|
|
LOGD("## encryptMessageJni(): IN ");
|
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
|
|
|
LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Session ptr=NULL";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aClearMsgBuffer)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
|
|
|
LOGE("## encryptMessageJni(): failure - invalid clear message");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid clear message";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!aEncryptedMsg)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
2016-10-13 19:21:01 +02:00
|
|
|
LOGE("## encryptMessageJni(): failure - invalid encrypted message");
|
2018-10-10 21:06:58 +02:00
|
|
|
errorMessage = "invalid encrypted message";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
2018-10-10 21:06:58 +02:00
|
|
|
else if (!(clearMsgPtr = env->GetByteArrayElements(aClearMsgBuffer, &clearMsgIsCopied)))
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
|
|
|
LOGE("## encryptMessageJni(): failure - clear message JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "clear message JNI allocation OOM";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg)))
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
|
|
|
LOGE("## encryptMessageJni(): failure - unable to get crypted message class");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "unable to get crypted message class";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J")))
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
|
|
|
LOGE("## encryptMessageJni(): failure - unable to get message type field");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "unable to get message type field";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-11 15:53:49 +02:00
|
|
|
// get message type
|
|
|
|
size_t messageType = olm_encrypt_message_type(sessionPtr);
|
2017-01-03 09:32:59 +01:00
|
|
|
uint8_t *randomBuffPtr = NULL;
|
2016-10-11 15:53:49 +02:00
|
|
|
|
2016-10-06 19:55:03 +02:00
|
|
|
// compute random buffer
|
2016-10-11 15:53:49 +02:00
|
|
|
// Note: olm_encrypt_random_length() can return 0, which means
|
|
|
|
// it just does not need new random data to encrypt a new message
|
2016-10-06 19:55:03 +02:00
|
|
|
size_t randomLength = olm_encrypt_random_length(sessionPtr);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## encryptMessageJni(): 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-06 19:55:03 +02:00
|
|
|
{
|
|
|
|
LOGE("## encryptMessageJni(): failure - random buffer init");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "random buffer init";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// alloc buffer for encrypted message
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer);
|
2016-10-06 19:55:03 +02:00
|
|
|
size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
|
2017-01-09 15:03:15 +01:00
|
|
|
void *encryptedMsgPtr = malloc(encryptedMsgLength*sizeof(uint8_t));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (!encryptedMsgPtr)
|
2016-10-06 19:55:03 +02:00
|
|
|
{
|
2016-10-11 15:53:49 +02:00
|
|
|
LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "encryptedMsgPtr buffer OOM";
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
else
|
2016-10-11 15:53:49 +02:00
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
if (0 == randomLength)
|
2016-10-11 15:53:49 +02:00
|
|
|
{
|
|
|
|
LOGW("## encryptMessageJni(): random buffer is not required");
|
|
|
|
}
|
|
|
|
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## encryptMessageJni(): messageType=%lu randomLength=%lu clearMsgLength=%lu encryptedMsgLength=%lu",static_cast<long unsigned int>(messageType),static_cast<long unsigned int>(randomLength), static_cast<long unsigned int>(clearMsgLength), static_cast<long unsigned int>(encryptedMsgLength));
|
2016-10-11 15:53:49 +02:00
|
|
|
// encrypt message
|
2016-10-07 17:35:27 +02:00
|
|
|
size_t result = olm_encrypt(sessionPtr,
|
|
|
|
(void const *)clearMsgPtr,
|
|
|
|
clearMsgLength,
|
|
|
|
randomBuffPtr,
|
|
|
|
randomLength,
|
|
|
|
encryptedMsgPtr,
|
|
|
|
encryptedMsgLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (result == olm_error())
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = (const char *)olm_session_last_error(sessionPtr);
|
|
|
|
LOGE("## encryptMessageJni(): failure - Msg=%s", errorMessage);
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// update message type: PRE KEY or normal
|
|
|
|
env->SetLongField(aEncryptedMsg, typeMsgFieldId, (jlong)messageType);
|
2016-10-06 19:55:03 +02:00
|
|
|
|
2017-01-09 15:03:15 +01:00
|
|
|
encryptedMsgRet = env->NewByteArray(encryptedMsgLength);
|
|
|
|
env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedMsgLength, (jbyte*)encryptedMsgPtr);
|
2016-10-06 19:55:03 +02:00
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD("## encryptMessageJni(): success - result=%lu Type=%lu encryptedMsg=%.*s", static_cast<long unsigned int>(result), static_cast<unsigned long int>(messageType), static_cast<int>(result), (const char*)encryptedMsgPtr);
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
free(encryptedMsgPtr);
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
|
2017-01-10 14:45:20 +01:00
|
|
|
memset(randomBuffPtr, 0, randomLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
free(randomBuffPtr);
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (clearMsgPtr)
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2018-10-10 21:06:58 +02:00
|
|
|
if (clearMsgIsCopied)
|
|
|
|
{
|
|
|
|
memset(clearMsgPtr, 0, (size_t)env->GetArrayLength(aClearMsgBuffer));
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aClearMsgBuffer, clearMsgPtr, JNI_ABORT);
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 14:35:08 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
2017-01-09 15:03:15 +01:00
|
|
|
|
|
|
|
return encryptedMsgRet;
|
2016-10-06 19:55:03 +02:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:35:27 +02:00
|
|
|
/**
|
2016-10-11 15:53:49 +02:00
|
|
|
* Decrypt a message using the session.<br>
|
2017-01-10 11:40:57 +01:00
|
|
|
* An exception is thrown if the operation fails.
|
2016-10-07 17:35:27 +02:00
|
|
|
* @param aEncryptedMsg message to decrypt
|
2017-01-04 14:35:08 +01:00
|
|
|
* @return decrypted message if operation succeed
|
2016-10-07 17:35:27 +02:00
|
|
|
*/
|
2017-01-03 16:12:20 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg)
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
const char* errorMessage = NULL;
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
jbyteArray decryptedMsgRet = 0;
|
|
|
|
|
2016-10-20 17:40:20 +02:00
|
|
|
jclass encryptedMsgJClass = 0;
|
2016-10-07 17:35:27 +02:00
|
|
|
jstring encryptedMsgJstring = 0; // <= obtained from encryptedMsgFieldId
|
|
|
|
// field IDs
|
|
|
|
jfieldID encryptedMsgFieldId;
|
|
|
|
jfieldID typeMsgFieldId;
|
|
|
|
// ptrs
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession *sessionPtr = getSessionInstanceId(env, thiz);
|
2016-10-07 17:35:27 +02:00
|
|
|
const char *encryptedMsgPtr = NULL; // <= obtained from encryptedMsgJstring
|
2016-11-14 11:56:37 +01:00
|
|
|
uint8_t *plainTextMsgPtr = NULL;
|
2016-10-07 17:35:27 +02:00
|
|
|
char *tempEncryptedPtr = NULL;
|
|
|
|
|
2016-11-14 11:56:37 +01:00
|
|
|
LOGD("## decryptMessageJni(): IN - OlmSession");
|
2016-10-07 17:35:27 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-18 15:59:36 +02:00
|
|
|
LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Session ptr=NULL";
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!aEncryptedMsg)
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-18 15:59:36 +02:00
|
|
|
LOGE("## decryptMessageJni(): failure - invalid encrypted message");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid encrypted message";
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg)))
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-18 15:59:36 +02:00
|
|
|
LOGE("## decryptMessageJni(): failure - unable to get encrypted message class");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "unable to get encrypted message class";
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;")))
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-18 15:59:36 +02:00
|
|
|
LOGE("## decryptMessageJni(): failure - unable to get message field");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "unable to get message field";
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J")))
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-18 15:59:36 +02:00
|
|
|
LOGE("## decryptMessageJni(): failure - unable to get message type field");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "unable to get message type field";
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId)))
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-18 15:59:36 +02:00
|
|
|
LOGE("## decryptMessageJni(): failure - JNI encrypted object ");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "JNI encrypted object";
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
else if (!(encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0)))
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-18 15:59:36 +02:00
|
|
|
LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "encrypted message JNI allocation OOM";
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get message type
|
2016-10-13 00:19:47 +02:00
|
|
|
size_t encryptedMsgType = (size_t)env->GetLongField(aEncryptedMsg, typeMsgFieldId);
|
2016-10-07 17:35:27 +02:00
|
|
|
// get encrypted message length
|
2016-10-13 00:19:47 +02:00
|
|
|
size_t encryptedMsgLength = (size_t)env->GetStringUTFLength(encryptedMsgJstring);
|
2016-10-07 17:35:27 +02:00
|
|
|
|
|
|
|
// create a dedicated temp buffer to be used in next Olm API calls
|
2016-10-11 15:53:49 +02:00
|
|
|
tempEncryptedPtr = static_cast<char*>(malloc(encryptedMsgLength*sizeof(uint8_t)));
|
2016-10-07 17:35:27 +02:00
|
|
|
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
|
2017-01-10 14:45:20 +01:00
|
|
|
LOGD("## decryptMessageJni(): MsgType=%lu encryptedMsgLength=%lu encryptedMsg=%.*s",static_cast<long unsigned int>(encryptedMsgType),static_cast<long unsigned int>(encryptedMsgLength), static_cast<int>(encryptedMsgLength), encryptedMsgPtr);
|
2016-10-07 17:35:27 +02:00
|
|
|
|
|
|
|
// get max plaintext length
|
2016-10-11 15:53:49 +02:00
|
|
|
size_t maxPlainTextLength = olm_decrypt_max_plaintext_length(sessionPtr,
|
2016-10-13 00:19:47 +02:00
|
|
|
static_cast<size_t>(encryptedMsgType),
|
2016-10-11 15:53:49 +02:00
|
|
|
static_cast<void*>(tempEncryptedPtr),
|
2016-10-07 17:35:27 +02:00
|
|
|
encryptedMsgLength);
|
2016-10-11 15:53:49 +02:00
|
|
|
// Note: tempEncryptedPtr is destroyed by olm_decrypt_max_plaintext_length()
|
2016-10-07 17:35:27 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (maxPlainTextLength == olm_error())
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = (const char *)olm_session_last_error(sessionPtr);
|
|
|
|
LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s", errorMessage);
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",static_cast<long unsigned int>(maxPlainTextLength));
|
2016-10-11 15:53:49 +02:00
|
|
|
|
2016-10-07 17:35:27 +02:00
|
|
|
// allocate output decrypted message
|
2017-01-09 15:03:15 +01:00
|
|
|
plainTextMsgPtr = static_cast<uint8_t*>(malloc(maxPlainTextLength*sizeof(uint8_t)));
|
2016-10-07 17:35:27 +02:00
|
|
|
|
2016-10-11 15:53:49 +02:00
|
|
|
// decrypt, but before reload encrypted buffer (previous one was destroyed)
|
2016-10-07 17:35:27 +02:00
|
|
|
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
|
|
|
|
size_t plaintextLength = olm_decrypt(sessionPtr,
|
|
|
|
encryptedMsgType,
|
2016-10-13 19:21:01 +02:00
|
|
|
(void*)tempEncryptedPtr,
|
2016-10-07 17:35:27 +02:00
|
|
|
encryptedMsgLength,
|
2016-10-13 00:19:47 +02:00
|
|
|
plainTextMsgPtr,
|
2016-10-11 15:53:49 +02:00
|
|
|
maxPlainTextLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (plaintextLength == olm_error())
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = (const char *)olm_session_last_error(sessionPtr);
|
|
|
|
LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s", errorMessage);
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-03 16:12:20 +01:00
|
|
|
decryptedMsgRet = env->NewByteArray(plaintextLength);
|
|
|
|
env->SetByteArrayRegion(decryptedMsgRet, 0 , plaintextLength, (jbyte*)plainTextMsgPtr);
|
2017-01-03 10:46:56 +01:00
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
LOGD(" ## decryptMessageJni(): UTF-8 Conversion - decrypted returnedLg=%lu OK",static_cast<long unsigned int>(plaintextLength));
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
2018-10-10 21:06:58 +02:00
|
|
|
|
|
|
|
memset(plainTextMsgPtr, 0, maxPlainTextLength);
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (encryptedMsgPtr)
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
|
|
|
env->ReleaseStringUTFChars(encryptedMsgJstring, encryptedMsgPtr);
|
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (tempEncryptedPtr)
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
|
|
|
free(tempEncryptedPtr);
|
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (plainTextMsgPtr)
|
2016-10-07 17:35:27 +02:00
|
|
|
{
|
2016-10-11 15:53:49 +02:00
|
|
|
free(plainTextMsgPtr);
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 14:35:08 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
return decryptedMsgRet;
|
2016-10-07 17:35:27 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 18:25:09 +02:00
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Get the session identifier for this session.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @return the session identifier
|
|
|
|
*/
|
2017-01-03 16:12:20 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz)
|
2016-10-05 18:25:09 +02:00
|
|
|
{
|
2017-01-04 18:30:35 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-03 16:12:20 +01:00
|
|
|
jbyteArray returnValue = 0;
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGD("## getSessionIdentifierJni(): IN ");
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmSession *sessionPtr = getSessionInstanceId(env, thiz);
|
2016-10-18 15:59:36 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!sessionPtr)
|
|
|
|
{
|
|
|
|
LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid Session ptr=NULL";
|
2017-01-03 09:32:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get the size to alloc to contain the id
|
|
|
|
size_t lengthSessionId = olm_session_id_length(sessionPtr);
|
|
|
|
LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
|
|
|
|
|
2017-01-09 15:03:15 +01:00
|
|
|
void *sessionIdPtr = malloc(lengthSessionId*sizeof(uint8_t));
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
if (!sessionIdPtr)
|
|
|
|
{
|
|
|
|
LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "identifier allocation OOM";
|
2017-01-03 09:32:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId);
|
|
|
|
|
|
|
|
if (result == olm_error())
|
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = (const char *)olm_session_last_error(sessionPtr);
|
|
|
|
LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s", errorMessage);
|
2017-01-03 09:32:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%.*s",static_cast<long unsigned int>(result), static_cast<int>(result), (char*)sessionIdPtr);
|
2017-01-03 16:12:20 +01:00
|
|
|
|
|
|
|
returnValue = env->NewByteArray(result);
|
|
|
|
env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr);
|
2017-01-03 09:32:59 +01:00
|
|
|
}
|
2017-01-03 16:12:20 +01:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
free(sessionIdPtr);
|
|
|
|
}
|
|
|
|
}
|
2016-10-05 18:25:09 +02:00
|
|
|
|
2017-01-04 14:35:08 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
return returnValue;
|
2016-10-05 18:25:09 +02:00
|
|
|
}
|
|
|
|
|
2016-10-21 18:09:20 +02:00
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Serialize and encrypt session instance.<br>
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @param aKeyBuffer key used to encrypt the serialized account data
|
|
|
|
* @return the serialised account as bytes buffer.
|
|
|
|
**/
|
2017-01-04 18:30:35 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer)
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-04 18:30:35 +01:00
|
|
|
jbyteArray returnValue = 0;
|
2017-01-04 14:35:08 +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-09 10:01:01 +01:00
|
|
|
OlmSession* sessionPtr = getSessionInstanceId(env, thiz);
|
2016-10-21 18:09:20 +02:00
|
|
|
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD("## serializeJni(): IN");
|
2016-10-21 18:09:20 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - invalid session ptr");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid session ptr";
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aKeyBuffer)
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - invalid key");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "invalid key";
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
2018-10-17 21:50:36 +02:00
|
|
|
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied)))
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "ikeyPtr JNI allocation OOM";
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t pickledLength = olm_pickle_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 18:09: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 18:09:20 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM");
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = "pickledPtr buffer OOM";
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t result = olm_pickle_session(sessionPtr,
|
|
|
|
(void const *)keyPtr,
|
|
|
|
keyLength,
|
|
|
|
(void*)pickledPtr,
|
|
|
|
pickledLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (result == olm_error())
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-04 14:35:08 +01:00
|
|
|
errorMessage = olm_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## serializeJni(): failure - olm_pickle_session() Msg=%s", errorMessage);
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 14:45:20 +01:00
|
|
|
LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(pickledLength), 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 18:09:20 +02:00
|
|
|
}
|
2017-01-03 09:32:59 +01:00
|
|
|
|
|
|
|
free(pickledPtr);
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (keyPtr)
|
2016-10-21 18:09: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 18:09:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 14:35:08 +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 18:09: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_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer)
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
const char* errorMessage = NULL;
|
|
|
|
OlmSession* sessionPtr = initializeSessionMemory();
|
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 18:09:20 +02:00
|
|
|
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD("## deserializeJni(): IN");
|
2016-10-21 18:09:20 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-21 18:09: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 18:09:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aKeyBuffer)
|
2016-10-21 18:09: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 18:09:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aSerializedDataBuffer)
|
2016-10-21 18:09: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 = "serialized data";
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
2018-10-17 21:50:36 +02:00
|
|
|
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied)))
|
2016-10-21 18:09: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 18:09:20 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0)))
|
2016-10-21 18:09: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 18:09:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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 18:09:20 +02:00
|
|
|
|
|
|
|
size_t result = olm_unpickle_session(sessionPtr,
|
|
|
|
(void const *)keyPtr,
|
|
|
|
keyLength,
|
|
|
|
(void*)pickledPtr,
|
|
|
|
pickledLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (result == olm_error())
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = olm_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## deserializeJni(): failure - olm_unpickle_account() Msg=%s", errorMessage);
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD(" ## initJni(): success - result=%lu ", static_cast<long unsigned int>(result));
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-03 09:32:59 +01:00
|
|
|
if (keyPtr)
|
2016-10-21 18:09: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 18:09:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (pickledPtr)
|
2016-10-21 18:09:20 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT);
|
2016-10-21 18:09:20 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
if (sessionPtr)
|
|
|
|
{
|
|
|
|
olm_clear_session(sessionPtr);
|
|
|
|
free(sessionPtr);
|
|
|
|
}
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (jlong)(intptr_t)sessionPtr;
|
2018-10-10 21:06:58 +02:00
|
|
|
}
|