From 7e69d96afcb05fed7d5b75b6cbe786c0c1a86965 Mon Sep 17 00:00:00 2001 From: pedroGitt Date: Mon, 17 Oct 2016 16:48:29 +0200 Subject: Add extra tests to check if the calling JAVA instance is the expected one --- .../OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h | 9 ++- .../olm-sdk/src/main/jni/olm_jni_helper.cpp | 76 ++++++++++++++++++++-- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h index 2bfb9f8..e1fea02 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -53,6 +52,14 @@ static const int ERROR_CODE_KO = -1; // constants static const int ACCOUNT_CREATION_RANDOM_MODULO = 256; + +// strings +static const char *CLASS_OLM_OUTBOUND_GROUP_SESSION = "org/matrix/olm/OlmOutboundGroupSession"; +static const char *CLASS_OLM_INBOUND_GROUP_SESSION = "org/matrix/olm/OlmInboundGroupSession"; +static const char *CLASS_OLM_SESSION = "org/matrix/olm/OlmSession"; +static const char *CLASS_OLM_ACCOUNT = "org/matrix/olm/OlmAccount"; +static const char *CLASS_OLM_UTILITY = "org/matrix/olm/OlmUtility"; + #ifdef __cplusplus extern "C" { #endif diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp index 1d64d75..c518578 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp @@ -70,10 +70,17 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong instanceId=-1; jfieldID instanceIdField; jclass loaderClass; + jclass requiredClass = 0; if(NULL!=aJniEnv) { - if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) + requiredClass = aJniEnv->FindClass(CLASS_OLM_ACCOUNT); + + if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) + { + LOGD("## getAccountInstanceId() failure - invalid instance of"); + } + else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmAccountId", "J"))) { @@ -110,10 +117,17 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong instanceId=-1; jfieldID instanceIdField; jclass loaderClass; + jclass requiredClass = 0; if(NULL!=aJniEnv) { - if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) + requiredClass = aJniEnv->FindClass(CLASS_OLM_SESSION); + + if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) + { + LOGD("## getSessionInstanceId() failure - invalid instance of"); + } + else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmSessionId", "J"))) { @@ -151,10 +165,17 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong instanceId=-1; jfieldID instanceIdField; jclass loaderClass; + jclass requiredClass = 0; if(NULL!=aJniEnv) { - if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) + requiredClass = aJniEnv->FindClass(CLASS_OLM_INBOUND_GROUP_SESSION); + + if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) + { + LOGD("## getInboundGroupSessionInstanceId() failure - invalid instance of"); + } + else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmInboundGroupSessionId", "J"))) { @@ -191,10 +212,57 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong instanceId=-1; jfieldID instanceIdField; jclass loaderClass; + jclass requiredClass = 0; + + if(NULL!=aJniEnv) + { + requiredClass = aJniEnv->FindClass(CLASS_OLM_OUTBOUND_GROUP_SESSION); + + if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) + { + LOGD("## getOutboundGroupSessionInstanceId() failure - invalid instance of"); + } + else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) + { + if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmOutboundGroupSessionId", "J"))) + { + instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); + aJniEnv->DeleteLocalRef(loaderClass); + } + else + { + LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetFieldID=null"); + } + } + else + { + LOGD("## getOutboundGroupSessionInstanceId() ERROR! GetObjectClass=null"); + } + } + else + { + LOGD("## getOutboundGroupSessionInstanceId() ERROR! aJniEnv=NULL"); + } + + return instanceId; +} + +jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) +{ + jlong instanceId=-1; + jfieldID instanceIdField; + jclass loaderClass; + jclass requiredClass = 0; if(NULL!=aJniEnv) { - if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) + requiredClass = aJniEnv->FindClass(CLASS_OLM_UTILITY); + + if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) + { + LOGD("## getOutboundGroupSessionInstanceId() failure - invalid instance of"); + } + else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject))) { if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeOlmOutboundGroupSessionId", "J"))) { -- cgit v1.2.3