Commit 45a98c20 authored by ylecollen's avatar ylecollen
Browse files

EncryptMessage triggers an exception when it fails

parent 9552e14f
......@@ -113,7 +113,11 @@ public class OlmGroupSessionTest {
@Test
public void test05OutboundGroupEncryptMessage() {
// alice encrypts a message to bob
mAliceToBobMessage = mAliceOutboundGroupSession.encryptMessage(CLEAR_MESSAGE1);
try {
mAliceToBobMessage = mAliceOutboundGroupSession.encryptMessage(CLEAR_MESSAGE1);
} catch (OlmOutboundGroupSession.EncryptMessageException e) {
assertTrue("Exception in bob encryptMessage, Exception code=" + e.getMessage(), false);
}
assertFalse(TextUtils.isEmpty(mAliceToBobMessage));
// test message index after encryption is incremented
......
......@@ -42,6 +42,15 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
*/
private transient long mNativeId;
/**
* Exception triggered in {@link #encryptMessage(String)}
*/
static public class EncryptMessageException extends Exception {
public EncryptMessageException(String message) {
super(message);
}
}
/**
* Constructor.<br>
* Create and save a new session native instance ID and
......@@ -252,22 +261,34 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
* Encrypt some plain-text message.<br>
* The message given as parameter is encrypted and returned as the return value.
* @param aClearMsg message to be encrypted
* @return the encrypted message if operation succeed, null otherwise
* @return the encrypted message
* @exception EncryptMessageException the encryption failure reason
*/
public String encryptMessage(String aClearMsg) {
public String encryptMessage(String aClearMsg) throws EncryptMessageException {
String retValue = null;
if(!TextUtils.isEmpty(aClearMsg)) {
if (!TextUtils.isEmpty(aClearMsg)) {
StringBuffer errorMsg = new StringBuffer();
try {
retValue = new String(encryptMessageJni(aClearMsg.getBytes("UTF-8")), "UTF-8");
byte[] encryptedBuffer = encryptMessageJni(aClearMsg.getBytes("UTF-8"), errorMsg);
if (null != encryptedBuffer) {
retValue = new String(encryptedBuffer , "UTF-8");
}
} catch (Exception e) {
Log.e(LOG_TAG, "## encryptMessage() failed " + e.getMessage());
errorMsg.append(e.getMessage());
}
if (0 != errorMsg.length()) {
throw new EncryptMessageException(errorMsg.toString());
}
}
return retValue;
}
private native byte[] encryptMessageJni(byte[] aClearMsgBuffer);
private native byte[] encryptMessageJni(byte[] aClearMsgBuffer, StringBuffer aErrorMsg);
/**
* Return true the object resources have been released.<br>
......
......@@ -268,7 +268,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *
return returnValue;
}
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer)
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aErrorMsg)
{
LOGD("## encryptMessageJni(): IN");
......@@ -277,10 +277,17 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
OlmOutboundGroupSession *sessionPtr = NULL;
jbyte* clearMsgPtr = NULL;
jclass errorMsgJClass = 0;
jmethodID errorMsgMethodId = 0;
if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{
LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL");
}
else if (!aErrorMsg)
{
LOGE(" ## encryptMessageJni(): failure - invalid error output");
}
else if (!aClearMsgBuffer)
{
LOGE(" ## encryptMessageJni(): failure - invalid clear message");
......@@ -289,6 +296,14 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
{
LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM");
}
else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{
LOGE(" ## encryptMessageJni(): failure - unable to get error class");
}
else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{
LOGE(" ## encryptMessageJni(): failure - unable to get error method ID");
}
else
{
// get clear message length
......@@ -312,9 +327,19 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
clearMsgLength,
encryptedMsgPtr,
encryptedMsgLength);
if (encryptedLength == olm_error())
{
LOGE(" ## encryptMessageJni(): failure - olm_group_encrypt Msg=%s",(const char *)olm_outbound_group_session_last_error(sessionPtr));
const char * errorMsgPtr = olm_outbound_group_session_last_error(sessionPtr);
LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr);
jstring errorJstring = env->NewStringUTF(errorMsgPtr);
if (errorJstring)
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
}
else
{
......@@ -329,7 +354,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
free(encryptedMsgPtr);
}
}
}
// free alloc
if (clearMsgPtr)
......
......@@ -37,7 +37,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(J
JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer);
JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aErrorMsg);
// serialization
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment