Use a 4 spaces tabulation

This commit is contained in:
ylecollen 2017-01-02 16:02:17 +01:00
parent 8f3d5bed72
commit 47a52dcf41

View file

@ -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)
{
LOGE("## setRandomInBuffer(): failure - random size=0");
}
else if (!(*aBuffer2Ptr = (uint8_t*)malloc(bufferLen)))
{
LOGE("## setRandomInBuffer(): failure - alloc mem OOM");
}
else
{
LOGD("## setRandomInBuffer(): randomSize=%lu",static_cast<long unsigned int>(aRandomSize));
if (constructor) // use the secureRandom class
{ jclass cls = env->FindClass("java/security/SecureRandom");
newObj = env->NewObject(cls, constructor);
jbyteArray tempByteArray = env->NewByteArray(bufferLen);
if (newObj && tempByteArray) if (cls)
{ {
env->CallVoidMethod(newObj, nextByteMethod, tempByteArray); jobject newObj = 0;
jmethodID constructor = env->GetMethodID(cls, "<init>", "()V");
jmethodID nextByteMethod = env->GetMethodID(cls, "nextBytes", "([B)V");
jbyte* buffer = env->GetByteArrayElements(tempByteArray, NULL); if (constructor)
{
newObj = env->NewObject(cls, constructor);
jbyteArray tempByteArray = env->NewByteArray(bufferLen);
if (buffer) if (newObj && tempByteArray)
{ {
memcpy(*aBuffer2Ptr, buffer, bufferLen); env->CallVoidMethod(newObj, nextByteMethod, tempByteArray);
secureRandomSucceeds = true; jbyte* buffer = env->GetByteArrayElements(tempByteArray, NULL);
// clear tempByteArray to hide sensitive data. if (buffer)
memset(buffer, 0, bufferLen); {
env->SetByteArrayRegion(tempByteArray, 0, bufferLen, buffer); memcpy(*aBuffer2Ptr, buffer, bufferLen);
retCode = true;
// ensure that the buffer is released // clear tempByteArray to hide sensitive data.
env->ReleaseByteArrayElements(tempByteArray, buffer, JNI_ABORT); 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 (tempByteArray) // debug purpose
/*for(int i = 0; i < aRandomSize; i++)
{ {
env->DeleteLocalRef(tempByteArray); LOGD("## setRandomInBuffer(): randomBuffPtr[%ld]=%d",i, (*aBuffer2Ptr)[i]);
} }*/
if (newObj)
{
env->DeleteLocalRef(newObj);
}
}
} }
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)
**/ **/
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"); jclass requiredClass = aJniEnv->FindClass(aCallingClass);
} jclass loaderClass = 0;
else if(0 != (loaderClass=aJniEnv->GetObjectClass(aJavaObject)))
{ if (requiredClass && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass)))
if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeId", "J"))) {
{ LOGE("## getAccountInstanceId() failure - invalid instance of");
instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); }
LOGD("## getInstanceId(): read from java instanceId=%lld",instanceId); else if (loaderClass = aJniEnv->GetObjectClass(aJavaObject))
} {
else jfieldID instanceIdField = aJniEnv->GetFieldID(loaderClass, "mNativeId", "J");
{
LOGE("## getInstanceId() ERROR! GetFieldID=null"); 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); LOGD("## getInstanceId() success - instanceId=%p (jlong)(intptr_t)instanceId=%lld",(void*)instanceId, (jlong)(intptr_t)instanceId);
if (loaderClass) return instanceId;
{
aJniEnv->DeleteLocalRef(loaderClass);
}
return instanceId;
} }
/** /**
@ -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)
**/ **/
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)
**/ **/
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)
**/ **/
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)
**/ **/
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)
*/ */
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)) if (!aCStringMsgPtr || !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
jstring strEncode = (env)->NewStringUTF("UTF-8");
jclass jClass = env->FindClass("java/lang/String");
jmethodID cstor = env->GetMethodID(jClass, "<init>", "([BLjava/lang/String;)V");
if((0!=jClass) && (0!=jClass) && (0!=strEncode))
{ {
convertedRetValue = (jstring) env->NewObject(jClass, cstor, tempByteArray, strEncode); LOGE("## javaCStringToUtf8(): failure - invalid parameters (null)");
LOGD(" ## javaCStringToUtf8(): succeed"); }
env->DeleteLocalRef(tempByteArray); else if (!(tempByteArray=env->NewByteArray(aMsgLength)))
{
LOGE("## javaCStringToUtf8(): failure - return byte array OOM");
} }
else else
{ {
LOGE(" ## javaCStringToUtf8(): failure - invalid Java references"); env->SetByteArrayRegion(tempByteArray, 0, aMsgLength, (const jbyte*)aCStringMsgPtr);
}
}
return convertedRetValue; // 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;
} }