EncryptMessage triggers an exception when it fails

This commit is contained in:
ylecollen 2017-01-03 16:41:04 +01:00
parent 9552e14fda
commit 45a98c20a8
4 changed files with 60 additions and 10 deletions

View file

@ -113,7 +113,11 @@ public class OlmGroupSessionTest {
@Test @Test
public void test05OutboundGroupEncryptMessage() { public void test05OutboundGroupEncryptMessage() {
// alice encrypts a message to bob // alice encrypts a message to bob
try {
mAliceToBobMessage = mAliceOutboundGroupSession.encryptMessage(CLEAR_MESSAGE1); mAliceToBobMessage = mAliceOutboundGroupSession.encryptMessage(CLEAR_MESSAGE1);
} catch (OlmOutboundGroupSession.EncryptMessageException e) {
assertTrue("Exception in bob encryptMessage, Exception code=" + e.getMessage(), false);
}
assertFalse(TextUtils.isEmpty(mAliceToBobMessage)); assertFalse(TextUtils.isEmpty(mAliceToBobMessage));
// test message index after encryption is incremented // test message index after encryption is incremented

View file

@ -42,6 +42,15 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
*/ */
private transient long mNativeId; private transient long mNativeId;
/**
* Exception triggered in {@link #encryptMessage(String)}
*/
static public class EncryptMessageException extends Exception {
public EncryptMessageException(String message) {
super(message);
}
}
/** /**
* Constructor.<br> * Constructor.<br>
* Create and save a new session native instance ID and * 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> * Encrypt some plain-text message.<br>
* The message given as parameter is encrypted and returned as the return value. * The message given as parameter is encrypted and returned as the return value.
* @param aClearMsg message to be encrypted * @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; String retValue = null;
if(!TextUtils.isEmpty(aClearMsg)) { if (!TextUtils.isEmpty(aClearMsg)) {
StringBuffer errorMsg = new StringBuffer();
try { 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) { } catch (Exception e) {
Log.e(LOG_TAG, "## encryptMessage() failed " + e.getMessage()); Log.e(LOG_TAG, "## encryptMessage() failed " + e.getMessage());
errorMsg.append(e.getMessage());
}
if (0 != errorMsg.length()) {
throw new EncryptMessageException(errorMsg.toString());
} }
} }
return retValue; 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> * Return true the object resources have been released.<br>

View file

@ -268,7 +268,7 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *
return returnValue; 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"); LOGD("## encryptMessageJni(): IN");
@ -277,10 +277,17 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
OlmOutboundGroupSession *sessionPtr = NULL; OlmOutboundGroupSession *sessionPtr = NULL;
jbyte* clearMsgPtr = NULL; jbyte* clearMsgPtr = NULL;
jclass errorMsgJClass = 0;
jmethodID errorMsgMethodId = 0;
if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz)))
{ {
LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL"); LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL");
} }
else if (!aErrorMsg)
{
LOGE(" ## encryptMessageJni(): failure - invalid error output");
}
else if (!aClearMsgBuffer) else if (!aClearMsgBuffer)
{ {
LOGE(" ## encryptMessageJni(): failure - invalid clear message"); 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"); 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 else
{ {
// get clear message length // get clear message length
@ -312,9 +327,19 @@ JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIE
clearMsgLength, clearMsgLength,
encryptedMsgPtr, encryptedMsgPtr,
encryptedMsgLength); encryptedMsgLength);
if (encryptedLength == olm_error()) 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 else
{ {

View file

@ -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 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(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 // serialization
JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg); JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeDataWithKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKey, jobject aErrorMsg);