Skip to content
Snippets Groups Projects
Commit 47a52dcf authored by ylecollen's avatar ylecollen
Browse files

Use a 4 spaces tabulation

parent 8f3d5bed
No related branches found
No related tags found
No related merge requests found
...@@ -29,99 +29,80 @@ using namespace AndroidOlmSdk; ...@@ -29,99 +29,80 @@ using namespace AndroidOlmSdk;
**/ **/
bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize) bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize)
{ {
bool retCode = false; bool retCode = false;
int bufferLen = aRandomSize*sizeof(uint8_t); int bufferLen = aRandomSize*sizeof(uint8_t);
if(NULL == aBuffer2Ptr) if (!aBuffer2Ptr)
{
LOGE("## setRandomInBuffer(): failure - aBuffer=NULL");
}
else if(0 == aRandomSize)
{
LOGE("## setRandomInBuffer(): failure - random size=0");
}
else if(NULL == (*aBuffer2Ptr = (uint8_t*)malloc(bufferLen)))
{
LOGE("## setRandomInBuffer(): failure - alloc mem OOM");
}
else
{
LOGD("## setRandomInBuffer(): randomSize=%lu",static_cast<long unsigned int>(aRandomSize));
bool secureRandomSucceeds = false;
// use the secureRandom class
jclass cls = env->FindClass("java/security/SecureRandom");
if (cls)
{ {
jobject newObj = 0; LOGE("## setRandomInBuffer(): failure - aBuffer=NULL");
jmethodID constructor = env->GetMethodID(cls, "<init>", "()V"); }
jmethodID nextByteMethod = env->GetMethodID(cls, "nextBytes", "([B)V"); else if(!aRandomSize)
{
if (constructor) LOGE("## setRandomInBuffer(): failure - random size=0");
{ }
newObj = env->NewObject(cls, constructor); else if (!(*aBuffer2Ptr = (uint8_t*)malloc(bufferLen)))
jbyteArray tempByteArray = env->NewByteArray(bufferLen); {
LOGE("## setRandomInBuffer(): failure - alloc mem OOM");
if (newObj && tempByteArray) }
{ else
env->CallVoidMethod(newObj, nextByteMethod, tempByteArray); {
LOGD("## setRandomInBuffer(): randomSize=%lu",static_cast<long unsigned int>(aRandomSize));
jbyte* buffer = env->GetByteArrayElements(tempByteArray, NULL);
if (buffer)
{
memcpy(*aBuffer2Ptr, buffer, bufferLen);
secureRandomSucceeds = true;
// clear tempByteArray to hide sensitive data.
memset(buffer, 0, bufferLen);
env->SetByteArrayRegion(tempByteArray, 0, bufferLen, buffer);
// ensure that the buffer is released // use the secureRandom class
env->ReleaseByteArrayElements(tempByteArray, buffer, JNI_ABORT); jclass cls = env->FindClass("java/security/SecureRandom");
}
}
if (tempByteArray) if (cls)
{ {
env->DeleteLocalRef(tempByteArray); jobject newObj = 0;
jmethodID constructor = env->GetMethodID(cls, "<init>", "()V");
jmethodID nextByteMethod = env->GetMethodID(cls, "nextBytes", "([B)V");
if (constructor)
{
newObj = env->NewObject(cls, constructor);
jbyteArray tempByteArray = env->NewByteArray(bufferLen);
if (newObj && tempByteArray)
{
env->CallVoidMethod(newObj, nextByteMethod, tempByteArray);
jbyte* buffer = env->GetByteArrayElements(tempByteArray, NULL);
if (buffer)
{
memcpy(*aBuffer2Ptr, buffer, bufferLen);
retCode = true;
// clear tempByteArray to hide sensitive data.
memset(buffer, 0, bufferLen);
env->SetByteArrayRegion(tempByteArray, 0, bufferLen, buffer);
// ensure that the buffer is released
env->ReleaseByteArrayElements(tempByteArray, buffer, JNI_ABORT);
}
}
if (tempByteArray)
{
env->DeleteLocalRef(tempByteArray);
}
if (newObj)
{
env->DeleteLocalRef(newObj);
}
}
} }
if (newObj) // debug purpose
/*for(int i = 0; i < aRandomSize; i++)
{ {
env->DeleteLocalRef(newObj); LOGD("## setRandomInBuffer(): randomBuffPtr[%ld]=%d",i, (*aBuffer2Ptr)[i]);
} }*/
}
} }
if (!secureRandomSucceeds) return retCode;
{
LOGE("## setRandomInBuffer(): SecureRandom failed, use a fallback");
struct timeval timeValue;
gettimeofday(&timeValue, NULL);
srand(timeValue.tv_usec); // init seed
for(size_t i=0;i<aRandomSize;i++)
{
(*aBuffer2Ptr)[i] = (uint8_t)(rand()%ACCOUNT_CREATION_RANDOM_MODULO);
}
}
// debug purpose
/*for(int i = 0; i < aRandomSize; i++)
{
LOGD("## setRandomInBuffer(): randomBuffPtr[%ld]=%d",i, (*aBuffer2Ptr)[i]);
}*/
retCode = true;
}
return retCode;
} }
/** /**
* Read the instance ID of the calling object. * Read the instance ID of the calling object.
* @param aJniEnv pointer pointing on the JNI function table * @param aJniEnv pointer pointing on the JNI function table
...@@ -131,49 +112,45 @@ bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize) ...@@ -131,49 +112,45 @@ bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize)
**/ **/
jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass) jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass)
{ {
jlong instanceId = 0; jlong instanceId = 0;
jfieldID instanceIdField = 0; if (aJniEnv)
jclass loaderClass = 0;
jclass requiredClass = 0;
if(NULL!=aJniEnv)
{
requiredClass = aJniEnv->FindClass(aCallingClass);
if((0 != requiredClass) && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
{
LOGE("## getAccountInstanceId() failure - invalid instance of");
}
else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{ {
if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeId", "J"))) jclass requiredClass = aJniEnv->FindClass(aCallingClass);
{ jclass loaderClass = 0;
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
LOGD("## getInstanceId(): read from java instanceId=%lld",instanceId); if (requiredClass && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
} {
else LOGE("## getAccountInstanceId() failure - invalid instance of");
{ }
LOGE("## getInstanceId() ERROR! GetFieldID=null"); else if (loaderClass = aJniEnv->GetObjectClass(aJavaObject))
} {
jfieldID instanceIdField = aJniEnv->GetFieldID(loaderClass, "mNativeId", "J");
if (instanceIdField)
{
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
LOGD("## getInstanceId(): read from java instanceId=%lld",instanceId);
}
else
{
LOGE("## getInstanceId() ERROR! GetFieldID=null");
}
aJniEnv->DeleteLocalRef(loaderClass);
}
else
{
LOGE("## getInstanceId() ERROR! GetObjectClass=null");
}
} }
else else
{ {
LOGE("## getInstanceId() ERROR! GetObjectClass=null"); LOGE("## getInstanceId() ERROR! aJniEnv=NULL");
} }
}
else
{
LOGE("## getInstanceId() ERROR! aJniEnv=NULL");
}
LOGD("## getInstanceId() success - instanceId=%p (jlong)(intptr_t)instanceId=%lld",(void*)instanceId, (jlong)(intptr_t)instanceId);
if (loaderClass) LOGD("## getInstanceId() success - instanceId=%p (jlong)(intptr_t)instanceId=%lld",(void*)instanceId, (jlong)(intptr_t)instanceId);
{
aJniEnv->DeleteLocalRef(loaderClass);
}
return instanceId; return instanceId;
} }
/** /**
...@@ -184,12 +161,10 @@ jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingCl ...@@ -184,12 +161,10 @@ jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingCl
**/ **/
jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_ACCOUNT); jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_ACCOUNT);
return instanceId; return instanceId;
} }
/** /**
* Read the session instance ID of the calling object (aJavaObject).<br> * Read the session instance ID of the calling object (aJavaObject).<br>
* @param aJniEnv pointer pointing on the JNI function table * @param aJniEnv pointer pointing on the JNI function table
...@@ -198,8 +173,8 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) ...@@ -198,8 +173,8 @@ jlong getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_SESSION); jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_SESSION);
return instanceId; return instanceId;
} }
/** /**
...@@ -210,11 +185,10 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) ...@@ -210,11 +185,10 @@ jlong getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_INBOUND_GROUP_SESSION); jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_INBOUND_GROUP_SESSION);
return instanceId; return instanceId;
} }
/** /**
* Read the outbound group session instance ID of the calling object (aJavaObject).<br> * Read the outbound group session instance ID of the calling object (aJavaObject).<br>
* @param aJniEnv pointer pointing on the JNI function table * @param aJniEnv pointer pointing on the JNI function table
...@@ -223,8 +197,8 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) ...@@ -223,8 +197,8 @@ jlong getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_OUTBOUND_GROUP_SESSION); jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_OUTBOUND_GROUP_SESSION);
return instanceId; return instanceId;
} }
/** /**
...@@ -235,11 +209,10 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) ...@@ -235,11 +209,10 @@ jlong getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
**/ **/
jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
{ {
jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_UTILITY); jlong instanceId = getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_UTILITY);
return instanceId; return instanceId;
} }
/** /**
* Convert a C string into a UTF-8 format string. * Convert a C string into a UTF-8 format string.
* The conversion is performed in JAVA side to workaround the issue in NewStringUTF(). * The conversion is performed in JAVA side to workaround the issue in NewStringUTF().
...@@ -247,37 +220,37 @@ jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) ...@@ -247,37 +220,37 @@ jlong getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject)
*/ */
jstring javaCStringToUtf8(JNIEnv *env, uint8_t *aCStringMsgPtr, size_t aMsgLength) jstring javaCStringToUtf8(JNIEnv *env, uint8_t *aCStringMsgPtr, size_t aMsgLength)
{ {
jstring convertedRetValue = 0; jstring convertedRetValue = 0;
jbyteArray tempByteArray = NULL; jbyteArray tempByteArray = NULL;
if((NULL == aCStringMsgPtr) || (NULL == env))
{
LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)");
}
else if(NULL == (tempByteArray=env->NewByteArray(aMsgLength)))
{
LOGE("## javaCStringToUtf8(): failure - return byte array OOM");
}
else
{
env->SetByteArrayRegion(tempByteArray, 0, aMsgLength, (const jbyte*)aCStringMsgPtr);
// UTF-8 conversion from JAVA if (!aCStringMsgPtr || !env)
jstring strEncode = (env)->NewStringUTF("UTF-8"); {
jclass jClass = env->FindClass("java/lang/String"); LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)");
jmethodID cstor = env->GetMethodID(jClass, "<init>", "([BLjava/lang/String;)V"); }
else if (!(tempByteArray=env->NewByteArray(aMsgLength)))
if((0!=jClass) && (0!=jClass) && (0!=strEncode))
{ {
convertedRetValue = (jstring) env->NewObject(jClass, cstor, tempByteArray, strEncode); LOGE("## javaCStringToUtf8(): failure - return byte array OOM");
LOGD(" ## javaCStringToUtf8(): succeed");
env->DeleteLocalRef(tempByteArray);
} }
else else
{ {
LOGE(" ## javaCStringToUtf8(): failure - invalid Java references"); env->SetByteArrayRegion(tempByteArray, 0, aMsgLength, (const jbyte*)aCStringMsgPtr);
// UTF-8 conversion from JAVA
jstring strEncode = (env)->NewStringUTF("UTF-8");
jclass jClass = env->FindClass("java/lang/String");
jmethodID cstor = env->GetMethodID(jClass, "<init>", "([BLjava/lang/String;)V");
if (jClass && strEncode)
{
convertedRetValue = (jstring) env->NewObject(jClass, cstor, tempByteArray, strEncode);
LOGD(" ## javaCStringToUtf8(): succeed");
env->DeleteLocalRef(tempByteArray);
}
else
{
LOGE(" ## javaCStringToUtf8(): failure - invalid Java references");
}
} }
}
return convertedRetValue; return convertedRetValue;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment