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_inbound_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 initializeInboundGroupSessionMemory().<br>
|
|
|
|
* This method MUST be called when java counter part account instance is done.
|
|
|
|
*/
|
|
|
|
JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz)
|
|
|
|
{
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmInboundGroupSession* sessionPtr = getInboundGroupSessionInstanceId(env,thiz);
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGD("## releaseSessionJni(): InBound group session IN");
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2017-01-03 09:32:59 +01:00
|
|
|
{
|
|
|
|
LOGE("## releaseSessionJni(): failure - invalid inbound group session instance");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
LOGD(" ## releaseSessionJni(): sessionPtr=%p", sessionPtr);
|
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_inbound_group_session(sessionPtr);
|
|
|
|
LOGD(" ## releaseSessionJni(): clear_inbound_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_inbound_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 inbound 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 (OlmInboundGroupSession* => jlong) platform independent.
|
|
|
|
* @param aSessionKeyBuffer session key from an outbound session
|
2017-01-18 15:33:14 +01:00
|
|
|
* @param isImported true when the session key has been retrieved from a backup
|
2017-01-10 11:40:57 +01:00
|
|
|
* @return the initialized OlmInboundGroupSession* instance or throw an exception it fails.
|
|
|
|
**/
|
2017-01-18 15:33:14 +01:00
|
|
|
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer, jboolean isImported)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-09 13:55:58 +01:00
|
|
|
const char* errorMessage = NULL;
|
2016-10-13 19:21:01 +02:00
|
|
|
OlmInboundGroupSession* sessionPtr = NULL;
|
2017-01-10 11:40:57 +01:00
|
|
|
jbyte* sessionKeyPtr = NULL;
|
2018-10-17 21:50:36 +02:00
|
|
|
jboolean sessionWasCopied = JNI_FALSE;
|
2017-01-10 11:40:57 +01:00
|
|
|
size_t sessionSize = olm_inbound_group_session_size();
|
2016-10-14 18:43:57 +02:00
|
|
|
|
2016-10-23 23:55:45 +02:00
|
|
|
LOGD("## createNewSessionJni(): inbound group session IN");
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-03 09:32:59 +01:00
|
|
|
if (!sessionSize)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-23 23:55:45 +02:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0");
|
2017-01-09 13:55:58 +01:00
|
|
|
errorMessage = "inbound group session size = 0";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2017-01-10 11:40:57 +01:00
|
|
|
else if (!(sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize)))
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-03 09:32:59 +01:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - inbound group session OOM");
|
2017-01-09 13:55:58 +01:00
|
|
|
errorMessage = "inbound group session OOM";
|
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aSessionKeyBuffer)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - invalid aSessionKey");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "invalid aSessionKey";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2018-10-17 21:50:36 +02:00
|
|
|
else if (!(sessionKeyPtr = env->GetByteArrayElements(aSessionKeyBuffer, &sessionWasCopied)))
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - session key JNI allocation OOM");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "Session key JNI allocation OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
sessionPtr = olm_inbound_group_session(sessionPtr);
|
|
|
|
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t sessionKeyLength = (size_t)env->GetArrayLength(aSessionKeyBuffer);
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD(" ## createNewSessionJni(): sessionKeyLength=%lu", static_cast<long unsigned int>(sessionKeyLength));
|
|
|
|
|
2017-01-18 15:33:14 +01:00
|
|
|
size_t sessionResult;
|
|
|
|
|
|
|
|
if (JNI_FALSE == isImported)
|
|
|
|
{
|
|
|
|
LOGD(" ## createNewSessionJni(): init");
|
|
|
|
sessionResult = olm_init_inbound_group_session(sessionPtr, (const uint8_t*)sessionKeyPtr, sessionKeyLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD(" ## createNewSessionJni(): import");
|
|
|
|
sessionResult = olm_import_inbound_group_session(sessionPtr, (const uint8_t*)sessionKeyPtr, sessionKeyLength);
|
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
if (sessionResult == olm_error())
|
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = olm_inbound_group_session_last_error(sessionPtr);
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGE(" ## createNewSessionJni(): failure - init inbound 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-10 11:40:57 +01:00
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
if (sessionKeyPtr)
|
|
|
|
{
|
2018-10-17 21:50:36 +02:00
|
|
|
if (sessionWasCopied) {
|
|
|
|
memset(sessionKeyPtr, 0, (size_t)env->GetArrayLength(aSessionKeyBuffer));
|
|
|
|
}
|
2017-01-10 11:40:57 +01:00
|
|
|
env->ReleaseByteArrayElements(aSessionKeyBuffer, sessionKeyPtr, JNI_ABORT);
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:35:15 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
// release the allocated session
|
|
|
|
if (sessionPtr)
|
|
|
|
{
|
|
|
|
olm_clear_inbound_group_session(sessionPtr);
|
|
|
|
free(sessionPtr);
|
|
|
|
}
|
2017-01-04 12:35:15 +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-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
/**
|
2017-01-10 11:40:57 +01:00
|
|
|
* Get a base64-encoded identifier for this inbound group session.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @return the base64-encoded identifier
|
|
|
|
*/
|
2017-01-03 16:12:20 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz);
|
2017-01-03 16:12:20 +01:00
|
|
|
jbyteArray returnValue = 0;
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
LOGD("## sessionIdentifierJni(): inbound group session IN");
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## sessionIdentifierJni(): failure - invalid inbound group session instance");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "invalid inbound 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_inbound_group_session_id_length(sessionPtr);
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId));
|
2016-10-14 15:27:20 +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-13 19:21:01 +02:00
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM");
|
|
|
|
errorMessage = "inbound group session identifier allocation OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-14 15:27:20 +02:00
|
|
|
size_t result = olm_inbound_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 12:35:15 +01:00
|
|
|
errorMessage = (const char *)olm_inbound_group_session_last_error(sessionPtr);
|
2016-10-23 23:55:45 +02:00
|
|
|
LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr));
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
LOGD(" ## sessionIdentifierJni(): success - inbound group session 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);
|
2016-10-14 15:27:20 +02:00
|
|
|
}
|
2017-01-03 16:12:20 +01:00
|
|
|
|
2016-10-14 15:27:20 +02:00
|
|
|
free(sessionIdPtr);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-04 12:35:15 +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
|
|
|
/**
|
|
|
|
* Decrypt a message.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @param aEncryptedMsg the encrypted message
|
|
|
|
* @param aDecryptMessageResult the decryptMessage information
|
|
|
|
* @return the decrypted message
|
|
|
|
*/
|
2017-01-04 12:35:15 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsgBuffer, jobject aDecryptionResult)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-03 16:12:20 +01:00
|
|
|
jbyteArray decryptedMsgBuffer = 0;
|
2017-01-04 12:35:15 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-03 16:12:20 +01:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz);
|
2017-01-03 14:14:56 +01:00
|
|
|
jbyte *encryptedMsgPtr = NULL;
|
2016-11-23 01:20:47 +01:00
|
|
|
jclass indexObjJClass = 0;
|
|
|
|
jfieldID indexMsgFieldId;
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGD("## decryptMessageJni(): inbound group session IN");
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## decryptMessageJni(): failure - invalid inbound group session ptr=NULL");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "invalid inbound group session ptr=NULL";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aEncryptedMsgBuffer)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## decryptMessageJni(): failure - invalid encrypted message");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "invalid encrypted message";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2017-01-03 10:46:56 +01:00
|
|
|
else if (!aDecryptionResult)
|
2016-11-23 01:20:47 +01:00
|
|
|
{
|
|
|
|
LOGE(" ## decryptMessageJni(): failure - invalid index object");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "invalid index object";
|
2016-11-28 11:56:20 +01:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(encryptedMsgPtr = env->GetByteArrayElements(aEncryptedMsgBuffer, 0)))
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## decryptMessageJni(): failure - encrypted message JNI allocation OOM");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "encrypted message JNI allocation OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
2017-01-03 10:46:56 +01:00
|
|
|
else if (!(indexObjJClass = env->GetObjectClass(aDecryptionResult)))
|
2016-11-23 01:20:47 +01:00
|
|
|
{
|
|
|
|
LOGE("## decryptMessageJni(): failure - unable to get index class");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "unable to get index class";
|
2016-11-23 01:20:47 +01:00
|
|
|
}
|
2017-01-02 17:10:59 +01:00
|
|
|
else if (!(indexMsgFieldId = env->GetFieldID(indexObjJClass,"mIndex","J")))
|
2016-11-23 01:20:47 +01:00
|
|
|
{
|
|
|
|
LOGE("## decryptMessageJni(): failure - unable to get index type field");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "unable to get index type field";
|
2016-11-23 01:20:47 +01:00
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// get encrypted message length
|
2017-01-03 14:14:56 +01:00
|
|
|
size_t encryptedMsgLength = (size_t)env->GetArrayLength(aEncryptedMsgBuffer);
|
2017-01-02 17:10:59 +01:00
|
|
|
uint8_t *tempEncryptedPtr = static_cast<uint8_t*>(malloc(encryptedMsgLength*sizeof(uint8_t)));
|
2016-10-13 19:21:01 +02:00
|
|
|
|
|
|
|
// create a dedicated temp buffer to be used in next Olm API calls
|
2017-01-02 17:10:59 +01:00
|
|
|
if (!tempEncryptedPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2016-10-14 18:43:57 +02:00
|
|
|
LOGE(" ## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "tempEncryptedPtr allocation OOM";
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
|
2017-01-10 14:45:20 +01:00
|
|
|
LOGD(" ## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%.*s",static_cast<long unsigned int>(encryptedMsgLength), static_cast<int>(encryptedMsgLength), encryptedMsgPtr);
|
2016-10-13 19:21:01 +02:00
|
|
|
|
|
|
|
// get max plaintext length
|
|
|
|
size_t maxPlainTextLength = olm_group_decrypt_max_plaintext_length(sessionPtr,
|
|
|
|
tempEncryptedPtr,
|
|
|
|
encryptedMsgLength);
|
2017-01-02 17:10:59 +01:00
|
|
|
if (maxPlainTextLength == olm_error())
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = olm_inbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s", errorMessage);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-24 17:21:28 +02:00
|
|
|
LOGD(" ## decryptMessageJni(): maxPlaintextLength=%lu",static_cast<long unsigned int>(maxPlainTextLength));
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2017-01-02 17:10:59 +01:00
|
|
|
uint32_t messageIndex = 0;
|
|
|
|
|
2016-10-13 19:21:01 +02:00
|
|
|
// allocate output decrypted message
|
2017-01-09 15:03:15 +01:00
|
|
|
uint8_t *plainTextMsgPtr = static_cast<uint8_t*>(malloc(maxPlainTextLength*sizeof(uint8_t)));
|
2016-10-13 19:21:01 +02:00
|
|
|
|
|
|
|
// decrypt, but before reload encrypted buffer (previous one was destroyed)
|
|
|
|
memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength);
|
|
|
|
size_t plaintextLength = olm_group_decrypt(sessionPtr,
|
|
|
|
tempEncryptedPtr,
|
|
|
|
encryptedMsgLength,
|
|
|
|
plainTextMsgPtr,
|
2016-11-23 01:20:47 +01:00
|
|
|
maxPlainTextLength,
|
|
|
|
&messageIndex);
|
2017-01-02 17:10:59 +01:00
|
|
|
if (plaintextLength == olm_error())
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = olm_inbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s", errorMessage);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-23 01:20:47 +01:00
|
|
|
// update index
|
2017-01-03 10:46:56 +01:00
|
|
|
env->SetLongField(aDecryptionResult, indexMsgFieldId, (jlong)messageIndex);
|
2016-11-23 01:20:47 +01:00
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
decryptedMsgBuffer = env->NewByteArray(plaintextLength);
|
|
|
|
env->SetByteArrayRegion(decryptedMsgBuffer, 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-13 19:21:01 +02:00
|
|
|
}
|
2017-01-02 17:10:59 +01:00
|
|
|
|
|
|
|
if (plainTextMsgPtr)
|
|
|
|
{
|
2017-01-09 15:12:15 +01:00
|
|
|
memset(plainTextMsgPtr, 0, maxPlainTextLength*sizeof(uint8_t));
|
2017-01-02 17:10:59 +01:00
|
|
|
free(plainTextMsgPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tempEncryptedPtr)
|
|
|
|
{
|
|
|
|
free(tempEncryptedPtr);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-02 17:10:59 +01:00
|
|
|
if (encryptedMsgPtr)
|
2016-10-13 19:21:01 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aEncryptedMsgBuffer, encryptedMsgPtr, JNI_ABORT);
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 12:35:15 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
return decryptedMsgBuffer;
|
2016-10-13 19:21:01 +02:00
|
|
|
}
|
|
|
|
|
2017-01-18 15:33:14 +01:00
|
|
|
/**
|
|
|
|
* Provides the first known index.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @return the first known index
|
|
|
|
*/
|
|
|
|
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(firstKnownIndexJni)(JNIEnv *env, jobject thiz)
|
|
|
|
{
|
|
|
|
const char* errorMessage = NULL;
|
|
|
|
OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz);
|
|
|
|
long returnValue = 0;
|
|
|
|
|
|
|
|
LOGD("## firstKnownIndexJni(): inbound group session IN");
|
|
|
|
|
|
|
|
if (!sessionPtr)
|
|
|
|
{
|
|
|
|
LOGE(" ## firstKnownIndexJni(): failure - invalid inbound group session instance");
|
|
|
|
errorMessage = "invalid inbound group session instance";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
returnValue = olm_inbound_group_session_first_known_index(sessionPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tells if the session is verified.
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @return true if the session is verified
|
|
|
|
*/
|
|
|
|
JNIEXPORT jboolean OLM_INBOUND_GROUP_SESSION_FUNC_DEF(isVerifiedJni)(JNIEnv *env, jobject thiz)
|
|
|
|
{
|
|
|
|
const char* errorMessage = NULL;
|
|
|
|
OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz);
|
|
|
|
jboolean returnValue = JNI_FALSE;
|
|
|
|
|
|
|
|
LOGD("## isVerifiedJni(): inbound group session IN");
|
|
|
|
|
|
|
|
if (!sessionPtr)
|
|
|
|
{
|
|
|
|
LOGE(" ## isVerifiedJni(): failure - invalid inbound group session instance");
|
|
|
|
errorMessage = "invalid inbound group session instance";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGE(" ## isVerifiedJni(): faaa %d", olm_inbound_group_session_is_verified(sessionPtr));
|
|
|
|
|
|
|
|
returnValue = (0 != olm_inbound_group_session_is_verified(sessionPtr)) ? JNI_TRUE : JNI_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports the session as byte array from a message index
|
|
|
|
* An exception is thrown if the operation fails.
|
|
|
|
* @param messageIndex key used to encrypt the serialized session data
|
|
|
|
* @return the session saved as bytes array
|
|
|
|
**/
|
|
|
|
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(exportJni)(JNIEnv *env, jobject thiz, jlong messageIndex) {
|
|
|
|
jbyteArray exportedByteArray = 0;
|
|
|
|
const char* errorMessage = NULL;
|
|
|
|
OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz);
|
|
|
|
|
|
|
|
LOGD("## exportJni(): inbound group session IN");
|
|
|
|
|
|
|
|
if (!sessionPtr)
|
|
|
|
{
|
|
|
|
LOGE(" ## exportJni (): failure - invalid inbound group session instance");
|
|
|
|
errorMessage = "invalid inbound group session instance";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t length = olm_export_inbound_group_session_length(sessionPtr);
|
|
|
|
|
|
|
|
LOGD(" ## exportJni(): length =%lu", static_cast<long unsigned int>(length));
|
|
|
|
|
|
|
|
void *bufferPtr = malloc(length * sizeof(uint8_t));
|
|
|
|
|
|
|
|
if (!bufferPtr)
|
|
|
|
{
|
|
|
|
LOGE(" ## exportJni(): failure - pickledPtr buffer OOM");
|
|
|
|
errorMessage = "pickledPtr buffer OOM";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t result = olm_export_inbound_group_session(sessionPtr,
|
|
|
|
(uint8_t*)bufferPtr,
|
|
|
|
length,
|
|
|
|
(long) messageIndex);
|
|
|
|
|
|
|
|
if (result == olm_error())
|
|
|
|
{
|
|
|
|
errorMessage = olm_inbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## exportJni(): failure - olm_export_inbound_group_session() Msg=%s", errorMessage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOGD(" ## exportJni(): success - result=%lu buffer=%.*s", static_cast<long unsigned int>(result), static_cast<int>(length), static_cast<char*>(bufferPtr));
|
|
|
|
|
|
|
|
exportedByteArray = env->NewByteArray(length);
|
|
|
|
env->SetByteArrayRegion(exportedByteArray, 0 , length, (jbyte*)bufferPtr);
|
|
|
|
|
|
|
|
// clean before leaving
|
|
|
|
memset(bufferPtr, 0, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(bufferPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return exportedByteArray;
|
|
|
|
}
|
2016-10-13 19:21:01 +02:00
|
|
|
|
2016-10-23 23:55:45 +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 aKeyBuffer key used to encrypt the serialized session data
|
|
|
|
* @return a base64 string if operation succeed, null otherwise
|
|
|
|
**/
|
2017-01-04 12:35:15 +01:00
|
|
|
JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer)
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
const char* errorMessage = NULL;
|
2017-01-03 16:12:20 +01:00
|
|
|
|
2017-01-04 12:35:15 +01:00
|
|
|
jbyteArray pickledDataRet = 0;
|
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
|
|
|
OlmInboundGroupSession* sessionPtr = getInboundGroupSessionInstanceId(env, thiz);
|
2016-10-23 23:55:45 +02:00
|
|
|
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD("## inbound group session serializeJni(): IN");
|
2016-10-23 23:55:45 +02:00
|
|
|
|
2017-01-09 10:01:01 +01:00
|
|
|
if (!sessionPtr)
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - invalid session ptr");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "invalid session ptr";
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aKeyBuffer)
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - invalid key");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "invalid key";
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
2018-10-17 21:50:36 +02:00
|
|
|
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied)))
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "keyPtr JNI allocation OOM";
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t pickledLength = olm_pickle_inbound_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-23 23:55:45 +02:00
|
|
|
|
2017-01-09 15:03:15 +01:00
|
|
|
void *pickledPtr = malloc(pickledLength*sizeof(uint8_t));
|
2017-01-02 17:10:59 +01:00
|
|
|
|
|
|
|
if (!pickledPtr)
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM");
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = "pickledPtr buffer OOM";
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t result = olm_pickle_inbound_group_session(sessionPtr,
|
|
|
|
(void const *)keyPtr,
|
|
|
|
keyLength,
|
|
|
|
(void*)pickledPtr,
|
|
|
|
pickledLength);
|
2017-01-03 09:32:59 +01:00
|
|
|
if (result == olm_error())
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-04 12:35:15 +01:00
|
|
|
errorMessage = olm_inbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## serializeJni(): failure - olm_pickle_outbound_group_session() Msg=%s", errorMessage);
|
2016-10-23 23:55:45 +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>(pickledLength), static_cast<char*>(pickledPtr));
|
2017-01-03 16:12:20 +01:00
|
|
|
|
|
|
|
pickledDataRet = env->NewByteArray(pickledLength);
|
|
|
|
env->SetByteArrayRegion(pickledDataRet, 0 , pickledLength, (jbyte*)pickledPtr);
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
2017-01-02 17:10:59 +01:00
|
|
|
|
|
|
|
free(pickledPtr);
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-02 17:10:59 +01:00
|
|
|
if (keyPtr)
|
2016-10-23 23:55:45 +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-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-04 12:35:15 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
2017-01-03 16:12:20 +01:00
|
|
|
return pickledDataRet;
|
2016-10-23 23:55:45 +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_INBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer)
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
const char* errorMessage = NULL;
|
|
|
|
|
|
|
|
OlmInboundGroupSession* sessionPtr = NULL;
|
|
|
|
size_t sessionSize = olm_inbound_group_session_size();
|
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-13 19:21:01 +02:00
|
|
|
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD("## deserializeJni(): IN");
|
2016-10-23 23:55:45 +02:00
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
if (!sessionSize)
|
|
|
|
{
|
|
|
|
LOGE(" ## deserializeJni(): failure - inbound group session size = 0");
|
|
|
|
errorMessage = "inbound group session size = 0";
|
|
|
|
}
|
|
|
|
else if (!(sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize)))
|
2016-10-23 23:55:45 +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-23 23:55:45 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aKeyBuffer)
|
2016-10-23 23:55:45 +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-23 23:55:45 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!aSerializedDataBuffer)
|
2016-10-23 23:55:45 +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-23 23:55:45 +02:00
|
|
|
}
|
2018-10-17 21:50:36 +02:00
|
|
|
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied)))
|
2016-10-23 23:55:45 +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-23 23:55:45 +02:00
|
|
|
}
|
2017-01-03 14:14:56 +01:00
|
|
|
else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0)))
|
2016-10-23 23:55:45 +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-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
sessionPtr = olm_inbound_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-23 23:55:45 +02:00
|
|
|
|
|
|
|
size_t result = olm_unpickle_inbound_group_session(sessionPtr,
|
|
|
|
(void const *)keyPtr,
|
|
|
|
keyLength,
|
|
|
|
(void*)pickledPtr,
|
|
|
|
pickledLength);
|
2017-01-02 17:10:59 +01:00
|
|
|
if (result == olm_error())
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-10 11:40:57 +01:00
|
|
|
errorMessage = olm_inbound_group_session_last_error(sessionPtr);
|
|
|
|
LOGE(" ## deserializeJni(): failure - olm_unpickle_inbound_group_session() Msg=%s", errorMessage);
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-04 08:57:41 +01:00
|
|
|
LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result));
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// free alloc
|
2017-01-02 17:10:59 +01:00
|
|
|
if (keyPtr)
|
2016-10-23 23:55:45 +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-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 17:10:59 +01:00
|
|
|
if (pickledPtr)
|
2016-10-23 23:55:45 +02:00
|
|
|
{
|
2017-01-03 14:14:56 +01:00
|
|
|
env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT);
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|
|
|
|
|
2017-01-10 11:40:57 +01:00
|
|
|
if (errorMessage)
|
|
|
|
{
|
|
|
|
if (sessionPtr)
|
|
|
|
{
|
|
|
|
olm_clear_inbound_group_session(sessionPtr);
|
|
|
|
free(sessionPtr);
|
|
|
|
}
|
|
|
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (jlong)(intptr_t)sessionPtr;
|
2016-10-23 23:55:45 +02:00
|
|
|
}
|