Update decryptMessage() API with the error message as an output parameter

This commit is contained in:
pedroGitt 2016-11-28 11:56:20 +01:00
parent 0263cd3039
commit ac0ccb224d
4 changed files with 82 additions and 11 deletions

View file

@ -151,8 +151,10 @@ public class OlmGroupSessionTest {
@Test @Test
public void test10InboundDecryptMessage() { public void test10InboundDecryptMessage() {
// test decrypted message // test decrypted message
StringBuffer errorMsg = new StringBuffer();
OlmInboundGroupSession.DecryptIndex index = new OlmInboundGroupSession.DecryptIndex(); OlmInboundGroupSession.DecryptIndex index = new OlmInboundGroupSession.DecryptIndex();
mBobDecryptedMessage = mBobInboundGroupSession.decryptMessage(mAliceToBobMessage, index);
mBobDecryptedMessage = mBobInboundGroupSession.decryptMessage(mAliceToBobMessage, index, errorMsg);
assertFalse(TextUtils.isEmpty(mBobDecryptedMessage)); assertFalse(TextUtils.isEmpty(mBobDecryptedMessage));
assertTrue(0==index.mIndex); assertTrue(0==index.mIndex);
} }
@ -437,11 +439,43 @@ public class OlmGroupSessionTest {
assertTrue("Exception in test18TestBadCharacterCrashInDecrypt, Exception code=" + e.getExceptionCode(), false); assertTrue("Exception in test18TestBadCharacterCrashInDecrypt, Exception code=" + e.getExceptionCode(), false);
} }
StringBuffer errorMsg = new StringBuffer();
OlmInboundGroupSession.DecryptIndex index = new OlmInboundGroupSession.DecryptIndex(); OlmInboundGroupSession.DecryptIndex index = new OlmInboundGroupSession.DecryptIndex();
String decryptedMessage = bobInboundGroupSession.decryptMessage(msgToDecryptWithEmoji, index);
String decryptedMessage = bobInboundGroupSession.decryptMessage(msgToDecryptWithEmoji, index, errorMsg);
assertNotNull(decryptedMessage); assertNotNull(decryptedMessage);
assertTrue(13==index.mIndex); assertTrue(13==index.mIndex);
} }
/**
* Specific test to check an error message is returned by decryptMessage() API.<br>
* A corrupted encrypted message is passed, and a INVALID_BASE64 is
* espexted.
**/
@Test
public void test19TestErrorMessageReturnedInDecrypt() {
OlmInboundGroupSession bobInboundGroupSession=null;
final String EXPECTED_ERROR_MESSAGE= "INVALID_BASE64";
StringBuffer errorMsg = new StringBuffer();
OlmInboundGroupSession.DecryptIndex index = new OlmInboundGroupSession.DecryptIndex();
String sessionKeyRef = "AgAAAAycZE6AekIctJWYxd2AWLOY15YmxZODm/WkgbpWkyycp6ytSp/R+wo84jRrzBNWmv6ySLTZ9R0EDOk9VI2eZyQ6Efdwyo1mAvrWvTkZl9yALPdkOIVHywyG65f1SNiLrnsln3hgsT1vUrISGyKtsljoUgQpr3JDPEhD0ilAi63QBjhnGCW252b+7nF+43rb6O6lwm93LaVwe2341Gdp6EkhTUvetALezEqDOtKN00wVqAbq0RQAnUJIowxHbMswg+FyoR1K1oCjnVEoF23O9xlAn5g1XtuBZP3moJlR2lwsBA";
String corruptedEncryptedMsg = "AwgANYTHINGf87ge45ge7gr*/rg5ganything4gr41rrgr4re55tanythingmcsXUkhDv0UePj922kgf+";
// valid INBOUND GROUP SESSION
try {
bobInboundGroupSession = new OlmInboundGroupSession(sessionKeyRef);
} catch (OlmException e) {
assertTrue("Exception in test19TestErrorMessageReturnedInDecrypt, Exception code=" + e.getExceptionCode(), false);
}
String decryptedMessage = bobInboundGroupSession.decryptMessage(corruptedEncryptedMsg, index, errorMsg);
assertTrue(0!=EXPECTED_ERROR_MESSAGE.length());
assertTrue(EXPECTED_ERROR_MESSAGE.equals(errorMsg.toString()));
assertTrue(null==decryptedMessage);
assertTrue(0==index.mIndex);
}
} }

View file

@ -45,7 +45,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
private transient long mNativeId; private transient long mNativeId;
/** /**
* Wrapper class to be used in {@link #decryptMessage(String, DecryptIndex)} * Wrapper class to be used in {@link #decryptMessage(String, DecryptIndex, StringBuffer)}
*/ */
static public class DecryptIndex { static public class DecryptIndex {
/** decrypt index **/ /** decrypt index **/
@ -142,16 +142,26 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
/** /**
* Decrypt the message passed in parameter. * Decrypt the message passed in parameter.<br>
* In case of error, null is returned and an error message description is provided in aErrorMsg.
* @param aEncryptedMsg the message to be decrypted * @param aEncryptedMsg the message to be decrypted
* @param aDecryptIndex_out decrypted message index * @param aDecryptIndex_out decrypted message index
* @param aErrorMsg error message description
* @return the decrypted message if operation succeed, null otherwise. * @return the decrypted message if operation succeed, null otherwise.
*/ */
public String decryptMessage(String aEncryptedMsg, DecryptIndex aDecryptIndex_out) { public String decryptMessage(String aEncryptedMsg, DecryptIndex aDecryptIndex_out, StringBuffer aErrorMsg) {
String decryptedMessage = decryptMessageJni(aEncryptedMsg, aDecryptIndex_out, OlmManager.ENABLE_STRING_UTF8_SPECIFIC_CONVERSION); String decryptedMessage = null;
// sanity check
if(null == aErrorMsg) {
Log.e(LOG_TAG,"## decryptMessage(): invalid parameter - aErrorMsg=null");
} else {
aErrorMsg.setLength(0);
decryptedMessage = decryptMessageJni(aEncryptedMsg, aDecryptIndex_out, aErrorMsg, OlmManager.ENABLE_STRING_UTF8_SPECIFIC_CONVERSION);
}
return decryptedMessage; return decryptedMessage;
} }
private native String decryptMessageJni(String aEncryptedMsg, DecryptIndex aDecryptIndex_out, boolean aIsUtf8ConversionRequired); private native String decryptMessageJni(String aEncryptedMsg, DecryptIndex aDecryptIndex_out, StringBuffer aErrorMsg, boolean aIsUtf8ConversionRequired);
/** /**

View file

@ -180,7 +180,7 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn
} }
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg, jobject aDecryptIndex, jboolean aIsUtf8ConversionRequired) JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg, jobject aDecryptIndex, jobject aErrorMsg, jboolean aIsUtf8ConversionRequired)
{ {
jstring decryptedMsgRetValue = 0; jstring decryptedMsgRetValue = 0;
OlmInboundGroupSession *sessionPtr = NULL; OlmInboundGroupSession *sessionPtr = NULL;
@ -190,6 +190,10 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
uint32_t messageIndex = 0; uint32_t messageIndex = 0;
jclass indexObjJClass = 0; jclass indexObjJClass = 0;
jfieldID indexMsgFieldId; jfieldID indexMsgFieldId;
jclass errorMsgJClass = 0;
jmethodID errorMsgMethodId = 0;
jstring errorJstring = 0;
const char *errorMsgPtr = NULL;
LOGD("## decryptMessageJni(): inbound group session IN"); LOGD("## decryptMessageJni(): inbound group session IN");
@ -205,6 +209,18 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
{ {
LOGE(" ## decryptMessageJni(): failure - invalid index object"); LOGE(" ## decryptMessageJni(): failure - invalid index object");
} }
else if(0 == aErrorMsg)
{
LOGE(" ## decryptMessageJni(): failure - invalid error object");
}
else if(0 == (errorMsgJClass = env->GetObjectClass(aErrorMsg)))
{
LOGE(" ## decryptMessageJni(): failure - unable to get error class");
}
else if(0 == (errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;")))
{
LOGE(" ## decryptMessageJni(): failure - unable to get error method ID");
}
else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0))) else if(0 == (encryptedMsgPtr = env->GetStringUTFChars(aEncryptedMsg, 0)))
{ {
LOGE(" ## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); LOGE(" ## decryptMessageJni(): failure - encrypted message JNI allocation OOM");
@ -238,7 +254,12 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
encryptedMsgLength); encryptedMsgLength);
if(maxPlainTextLength == olm_error()) if(maxPlainTextLength == olm_error())
{ {
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s",errorMsgPtr);
if(0 != (errorJstring = env->NewStringUTF(errorMsgPtr)))
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
} }
else else
{ {
@ -257,7 +278,13 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
&messageIndex); &messageIndex);
if(plaintextLength == olm_error()) if(plaintextLength == olm_error())
{ {
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); errorMsgPtr = olm_inbound_group_session_last_error(sessionPtr);
LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s",errorMsgPtr);
if(0 != (errorJstring = env->NewStringUTF(errorMsgPtr)))
{
env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring);
}
} }
else else
{ {

View file

@ -33,7 +33,7 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *
JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSessionKeyJni)(JNIEnv *env, jobject thiz, jstring aSessionKey); JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSessionKeyJni)(JNIEnv *env, jobject thiz, jstring aSessionKey);
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz);
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg, jobject aDecryptIndex, jboolean aIsUtf8ConversionRequired); JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jstring aEncryptedMsg, jobject aDecryptIndex, jobject aErrorMsg, jboolean aIsUtf8ConversionRequired);
// serialization // serialization
JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg); JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jstring aKey, jobject aErrorMsg);