aboutsummaryrefslogtreecommitdiff
path: root/java/android/OlmLibSdk/olm-sdk/src
diff options
context:
space:
mode:
authorylecollen <ylecollen@amdocs.com>2017-01-03 16:41:04 +0100
committerylecollen <ylecollen@amdocs.com>2017-01-03 16:41:04 +0100
commit45a98c20a8f0018c80b18e6bb6e04b3a6be03732 (patch)
tree97f21c3d24c7476b9ceff047de3e6b1e84f53d62 /java/android/OlmLibSdk/olm-sdk/src
parent9552e14fdadd9fadbe88970cfb8df7b697c0deb4 (diff)
EncryptMessage triggers an exception when it fails
Diffstat (limited to 'java/android/OlmLibSdk/olm-sdk/src')
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java6
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java31
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp31
-rw-r--r--java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h2
4 files changed, 60 insertions, 10 deletions
diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java
index 111032e..0f0d779 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java
@@ -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
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java
index 171891c..b9c21ff 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java
@@ -43,6 +43,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
* initialise a new outbound group session.<br>
@@ -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>
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp
index 211757f..aa0e5e7 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp
@@ -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)
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h
index 71ddb3b..37f0ea6 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.h
@@ -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);