From 102809955026d92a3f5cf29e05998d91a992de9c Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Fri, 14 Oct 2016 15:27:20 +0200 Subject: - Add inbound and outbound group sessions - Modify constructors for inbound and outbound group sessions - Add new Ecxception class --- .../src/main/jni/olm_outbound_group_session.cpp | 270 ++++++++++++--------- 1 file changed, 152 insertions(+), 118 deletions(-) (limited to 'java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp') diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp index 40af39d..46c0ee4 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp @@ -15,7 +15,6 @@ */ #include "olm_outbound_group_session.h" -#include "olm_utility.h" /** @@ -31,12 +30,12 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## releaseSessionJni(): failure - invalid inbound group session instance"); + LOGE("## releaseSessionJni(): failure - invalid outbound group session instance"); } else { size_t retCode = olm_clear_outbound_group_session(sessionPtr); - LOGD("## releaseSessionJni(): clear_inbound_group_session=%lu",retCode); + LOGD("## releaseSessionJni(): clear_outbound_group_session=%lu",retCode); LOGD("## releaseSessionJni(): IN"); free(sessionPtr); @@ -73,25 +72,26 @@ JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *e } /** - * Create a new outbound session.
+ * Start a new outbound session.
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise */ -JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz) +JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(JNIEnv *env, jobject thiz) { jint retCode = ERROR_CODE_KO; OlmOutboundGroupSession *sessionPtr = NULL; uint8_t *randomBuffPtr = NULL; - size_t sessionResult; + + LOGD("## initOutboundGroupSessionJni(): IN"); if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## initOutboundGroupSessionJni(): failure - invalid inbound group session instance"); + LOGE("## initOutboundGroupSessionJni(): failure - invalid outbound group session instance"); } else { // compute random buffer size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr); - + LOGW("## initOutboundGroupSessionJni(): randomLength=%lu",randomLength); if((0!=randomLength) && !setRandomInBuffer(&randomBuffPtr, randomLength)) { LOGE("## initOutboundGroupSessionJni(): failure - random buffer init"); @@ -103,190 +103,224 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(J LOGW("## initOutboundGroupSessionJni(): random buffer is not required"); } - size_t sessionResult = olm_init_outbound_group_session(sessionPtr, sessionKeyPtr, sessionKeyLength); + size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength); if(sessionResult == olm_error()) { - const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); + const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); + LOGE("## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s",errorMsgPtr); } else { retCode = ERROR_CODE_OK; - LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult); + LOGD("## initOutboundGroupSessionJni(): success - result=%lu", sessionResult); } - - } } - - else if(0 == aSessionKey) + if(NULL != randomBuffPtr) { - LOGE("## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"); + free(randomBuffPtr); } - else if(NULL == (sessionKeyPtr = (const uint8_t *)env->GetStringUTFChars(aSessionKey, 0))) + + return retCode; +} + +/** +* Get a base64-encoded identifier for this outbound group session. +*/ +JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) +{ + OlmOutboundGroupSession *sessionPtr = NULL; + uint8_t *sessionIdPtr = NULL; + jstring returnValueStr=0; + + LOGD("## sessionIdentifierJni(): outbound group session IN"); + + if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM"); + LOGE("## sessionIdentifierJni(): failure - invalid outbound group session instance"); } else { - size_t sessionKeyLength = (size_t)env->GetStringUTFLength(aSessionKey); - LOGD("## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu",sessionKeyLength); + // get the size to alloc + size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr); + LOGD("## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",lengthSessionId); - sessionResult = olm_init_inbound_group_session(sessionPtr, sessionKeyPtr, sessionKeyLength); - if(sessionResult == olm_error()) { - const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s",errorMsgPtr); + if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)))) + { + LOGE("## sessionIdentifierJni(): failure - outbound identifier allocation OOM"); } else { - retCode = ERROR_CODE_OK; - LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", sessionResult); - } - } + size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); + if (result == olm_error()) + { + const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); + LOGE("## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s",errorMsgPtr); + } + else + { + // update length + sessionIdPtr[result] = static_cast('\0'); + LOGD("## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId=%s",result, (char*)sessionIdPtr); + returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); + } - // free local alloc - if(NULL!= sessionKeyPtr) - { - env->ReleaseStringUTFChars(aSessionKey, (const char*)sessionKeyPtr); - } + // free alloc + free(sessionIdPtr); + } + } - return retCode; + return returnValueStr; } -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) +/** +* Get the current message index for this session.
+* Each message is sent with an increasing index, this +* method returns the index for the next message. +* @return current session index +*/ +JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz) { - OlmInboundGroupSession *sessionPtr = NULL; - uint8_t *sessionIdPtr = NULL; - jstring returnValueStr=0; + OlmOutboundGroupSession *sessionPtr = NULL; + jint indexRetValue = 0; - // get the size to alloc to contain the id - size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr); + LOGD("## messageIndexJni(): IN"); - if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) + if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) + { + LOGE("## messageIndexJni(): failure - invalid outbound group session instance"); + } + else { - LOGE("## sessionIdentifierJni(): failure - invalid inbound group session instance"); + indexRetValue = static_cast(olm_outbound_group_session_message_index(sessionPtr)); } - else if(NULL == (sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)))) + LOGD("## messageIndexJni(): success - index=%d",indexRetValue); + + return indexRetValue; +} + + +/** +* Get the base64-encoded current ratchet key for this session.
+*/ +JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz) +{ + OlmOutboundGroupSession *sessionPtr = NULL; + uint8_t *sessionKeyPtr = NULL; + jstring returnValueStr=0; + + LOGD("## sessionKeyJni(): outbound group session IN"); + + if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## sessionIdentifierJni(): failure - identifier allocation OOM"); + LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance"); } else { - size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); - if (result == olm_error()) + // get the size to alloc + size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr); + LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",sessionKeyLength); + + if(NULL == (sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t)))) { - const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## sessionIdentifierJni(): failure - get session identifier failure Msg=%s",errorMsgPtr); + LOGE(" ## sessionKeyJni(): failure - session key allocation OOM"); } else { - // update length - sessionIdPtr[result] = static_cast('\0'); + size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength); + if (result == olm_error()) + { + const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); + LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s",errorMsgPtr); + } + else + { + // update length + sessionKeyPtr[result] = static_cast('\0'); + LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%s",result, (char*)sessionKeyPtr); + returnValueStr = env->NewStringUTF((const char*)sessionKeyPtr); + } - LOGD("## sessionIdentifierJni(): success - result=%lu sessionId=%s",result, (char*)sessionIdPtr); - returnValueStr = env->NewStringUTF((const char*)sessionIdPtr); + // free alloc + free(sessionKeyPtr); } - free(sessionIdPtr); } return returnValueStr; } -JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg) + +JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jstring aClearMsg) { - jstring decryptedMsgRetValue = 0; - OlmInboundGroupSession *sessionPtr = NULL; - const char *encryptedMsgPtr = NULL; - uint8_t *plainTextMsgPtr = NULL; - uint8_t *tempEncryptedPtr = NULL; + jstring encryptedMsgRetValue = 0; + OlmOutboundGroupSession *sessionPtr = NULL; + const char *clearMsgPtr = NULL; + uint8_t *encryptedMsgPtr = NULL; - LOGD("## decryptMessageJni(): IN"); + LOGD("## encryptMessageJni(): IN"); - if(NULL == (sessionPtr = (OlmInboundGroupSession*)getInboundGroupSessionInstanceId(env,thiz))) + if(NULL == (sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) { - LOGE("## decryptMessageJni(): failure - invalid inbound group session ptr=NULL"); + LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL"); } - else if(0 == aEncryptedMsg) + else if(0 == aClearMsg) { - LOGE("## decryptMessageJni(): failure - invalid clear message"); + LOGE(" ## encryptMessageJni(): failure - invalid clear message"); } - else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0))) + else if(0 == (clearMsgPtr = env->GetStringUTFChars(aClearMsg, 0))) { - LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); + LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM"); } else { - // get encrypted message length - size_t encryptedMsgLength = (size_t)env->GetStringUTFLength(aEncryptedMsg); + // get clear message length + size_t clearMsgLength = (size_t)env->GetStringUTFLength(aClearMsg); + LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",clearMsgLength); - // create a dedicated temp buffer to be used in next Olm API calls - if(NULL == (tempEncryptedPtr = static_cast(malloc(encryptedMsgLength*sizeof(uint8_t))))) + // compute max encrypted length + size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength); + if(NULL == (encryptedMsgPtr = (uint8_t*)malloc(encryptedMsgLength*sizeof(uint8_t)))) { - LOGE("## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"); + LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); } else { - memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - LOGD("## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%s",encryptedMsgLength,encryptedMsgPtr); - - // get max plaintext length - size_t maxPlainTextLength = olm_group_decrypt_max_plaintext_length(sessionPtr, - tempEncryptedPtr, - encryptedMsgLength); - if(maxPlainTextLength == olm_error()) + LOGD(" ## encryptMessageJni(): estimated encryptedMsgLength=%lu",encryptedMsgLength); + + size_t decryptedLength = olm_group_encrypt(sessionPtr, + (uint8_t*)clearMsgPtr, + clearMsgLength, + encryptedMsgPtr, + encryptedMsgLength); + if(decryptedLength == olm_error()) { - const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr); + const char *errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr); + LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr); } else { - LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",maxPlainTextLength); - - // allocate output decrypted message - plainTextMsgPtr = static_cast(malloc(maxPlainTextLength*sizeof(uint8_t))); - - // decrypt, but before reload encrypted buffer (previous one was destroyed) - memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - size_t plaintextLength = olm_group_decrypt(sessionPtr, - tempEncryptedPtr, - encryptedMsgLength, - plainTextMsgPtr, - maxPlainTextLength); - if(plaintextLength == olm_error()) - { - const char *errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr); - } - else - { - // update decrypted buffer size - plainTextMsgPtr[plaintextLength] = static_cast('\0'); - - LOGD("## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",plaintextLength, (char*)plainTextMsgPtr); - decryptedMsgRetValue = env->NewStringUTF((const char*)plainTextMsgPtr); - } + // update decrypted buffer size + encryptedMsgPtr[decryptedLength] = static_cast('\0'); + + LOGD(" ## encryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s",decryptedLength, (char*)encryptedMsgPtr); + encryptedMsgRetValue = env->NewStringUTF((const char*)encryptedMsgPtr); } } - } + } // free alloc - if(NULL != encryptedMsgPtr) + if(NULL != clearMsgPtr) { - env->ReleaseStringUTFChars(aEncryptedMsg, encryptedMsgPtr); + env->ReleaseStringUTFChars(aClearMsg, clearMsgPtr); } - if(NULL != tempEncryptedPtr) - { - free(tempEncryptedPtr); - } - - if(NULL != plainTextMsgPtr) + if(NULL != encryptedMsgPtr) { - free(plainTextMsgPtr); + free(encryptedMsgPtr); } - return decryptedMsgRetValue; + return encryptedMsgRetValue; } -- cgit v1.2.3