From 88548f687e7f6f51a48ac9cf3a342354731ad694 Mon Sep 17 00:00:00 2001 From: ylecollen Date: Wed, 4 Jan 2017 11:46:37 +0100 Subject: OlmAccount methods trigger an exception when they fail. --- .../src/main/java/org/matrix/olm/OlmAccount.java | 118 +++++++++++------- .../src/main/java/org/matrix/olm/OlmException.java | 7 ++ .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp | 137 ++++++++++++++------- .../OlmLibSdk/olm-sdk/src/main/jni/olm_account.h | 8 +- .../OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h | 1 - 5 files changed, 175 insertions(+), 96 deletions(-) (limited to 'java/android/OlmLibSdk/olm-sdk/src/main') diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java index 649085e..3107ff3 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java @@ -63,9 +63,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { private transient long mNativeId; public OlmAccount() throws OlmException { - if(!initNewAccount()) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION); - } + initNewAccount(); } /** @@ -97,11 +95,14 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * Create and initialize a native account instance.
* Wrapper for {@link #initNewAccountJni()}. * To be called before any other API call. - * @return true if init succeed, false otherwise. + * @exception OlmException the failure reason */ - private boolean initNewAccount() { - mNativeId = initNewAccountJni(); - return (0 != mNativeId); + private void initNewAccount() throws OlmException { + try { + mNativeId = initNewAccountJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION, e.getMessage()); + } } /** @@ -147,10 +148,19 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * "ed25519":"+v8SOlOASFTMrX3MCKBM4iVnYoZ+JIjpNt1fi8Z9O2I" * } * @return identity keys dictionary if operation succeeds, null otherwise + * @exception OlmException the failure reason */ - public Map identityKeys() { + public Map identityKeys() throws OlmException { JSONObject identityKeysJsonObj = null; - byte[] identityKeysBuffer = identityKeysJni(); + + byte[] identityKeysBuffer; + + try { + identityKeysBuffer = identityKeysJni(); + } catch (Exception e) { + Log.e(LOG_TAG, "## identityKeys(): Failure - " + e.getMessage()); + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS, e.getMessage()); + } if (null != identityKeysBuffer) { try { @@ -165,6 +175,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { return toStringMap(identityKeysJsonObj); } + /** * Get the public identity keys (Ed25519 fingerprint key and Curve25519 identity key).
* Keys are Base64 encoded. @@ -180,6 +191,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { public long maxOneTimeKeys() { return maxOneTimeKeysJni(); } + private native long maxOneTimeKeysJni(); /** @@ -187,12 +199,17 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * by this account exceeds {@link #maxOneTimeKeys()}, the old keys are discarded.
* The corresponding keys are retrieved by {@link #oneTimeKeys()}. * @param aNumberOfKeys number of keys to generate - * @return 0 if operation succeed, -1 otherwise + * @exception OlmException the failure reason */ - public int generateOneTimeKeys(int aNumberOfKeys) { - return generateOneTimeKeysJni(aNumberOfKeys); + public void generateOneTimeKeys(int aNumberOfKeys) throws OlmException { + try { + generateOneTimeKeysJni(aNumberOfKeys); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS, e.getMessage()); + } } - private native int generateOneTimeKeysJni(int aNumberOfKeys); + + private native void generateOneTimeKeysJni(int aNumberOfKeys); /** * Return the "one time keys" in a dictionary.
@@ -207,11 +224,18 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { * }
* Public API for {@link #oneTimeKeysJni()}.
* Note: these keys are to be published on the server. - * @return one time keys in string dictionary if operation succeed, null otherwise + * @return one time keys in string dictionary. + * @exception OlmException the failure reason */ - public Map> oneTimeKeys() { + public Map> oneTimeKeys() throws OlmException { JSONObject oneTimeKeysJsonObj = null; - byte[] oneTimeKeysBuffer = oneTimeKeysJni(); + byte[] oneTimeKeysBuffer; + + try { + oneTimeKeysBuffer = oneTimeKeysJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS, e.getMessage()); + } if( null != oneTimeKeysBuffer) { try { @@ -226,6 +250,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { return toStringMapMap(oneTimeKeysJsonObj); } + /** * Get the public parts of the unpublished "one time keys" for the account.
* The returned data is a JSON-formatted object with the single property @@ -238,62 +263,67 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { /** * Remove the "one time keys" that the session used from the account. * @param aSession session instance - * @return 0 if operation succeed, 1 if no matching keys in the sessions to be removed, -1 if operation failed + * @return true if the operation succeeded. + * @throws OlmException the failure reason */ - public int removeOneTimeKeysForSession(OlmSession aSession) { - int retCode = -1; + public boolean removeOneTimeKeys(OlmSession aSession) throws OlmException { + boolean res = false; - if(null != aSession) { - retCode = removeOneTimeKeysForSessionJni(aSession.getOlmSessionId()); - Log.d(LOG_TAG,"## removeOneTimeKeysForSession(): result="+retCode); + if (null != aSession) { + try { + res = (removeOneTimeKeysJni(aSession.getOlmSessionId()) >= 0); + Log.d(LOG_TAG,"## removeOneTimeKeysForSession(): result=" + res); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS, e.getMessage()); + } } - return retCode; + return res; } + /** * Remove the "one time keys" that the session used from the account. * @param aNativeOlmSessionId native session instance identifier * @return 0 if operation succeed, 1 if no matching keys in the sessions to be removed, -1 if operation failed */ - private native int removeOneTimeKeysForSessionJni(long aNativeOlmSessionId); + private native int removeOneTimeKeysJni(long aNativeOlmSessionId); /** * Marks the current set of "one time keys" as being published. - * @return 0 if operation succeed, -1 otherwise + * @exception OlmException the failure reason */ - public int markOneTimeKeysAsPublished() { - return markOneTimeKeysAsPublishedJni(); + public void markOneTimeKeysAsPublished() throws OlmException { + try { + markOneTimeKeysAsPublishedJni(); + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED, e.getMessage()); + } } - private native int markOneTimeKeysAsPublishedJni(); + + private native void markOneTimeKeysAsPublishedJni(); /** * Sign a message with the ed25519 fingerprint key for this account.
* The signed message is returned by the method. * @param aMessage message to sign - * @return the signed message if operation succeed, null otherwise + * @return the signed message */ - public String signMessage(String aMessage) { + public String signMessage(String aMessage) throws OlmException { String result = null; if (null != aMessage) { - byte[] utf8String = null; - try { - utf8String = aMessage.getBytes("UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## signMessage(): failed =" + e.getMessage()); - } + byte[] utf8String = aMessage.getBytes("UTF-8"); - if (null != utf8String) { - byte[] signedMessage = signMessageJni(utf8String); + if (null != utf8String) { + byte[] signedMessage = signMessageJni(utf8String); - if (null != signedMessage) { - try { + if (null != signedMessage) { result = new String(signedMessage, "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## signMessage(): failed =" + e.getMessage()); } } + } catch (Exception e) { + throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE, e.getMessage()); } } @@ -407,7 +437,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { } else { aErrorMsg.setLength(0); try { - pickleRetValue = new String(serializeJni(aKey.getBytes("UTF-8"), aErrorMsg), "UTF-8"); + pickleRetValue = new String(serializeJni(aKey.getBytes("UTF-8")), "UTF-8"); } catch (Exception e) { Log.e(LOG_TAG, "## serialize() failed " + e.getMessage()); aErrorMsg.append(e.getMessage()); @@ -417,7 +447,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable { return pickleRetValue; } - private native byte[] serializeJni(byte[] aKey, StringBuffer aErrorMsg); + private native byte[] serializeJni(byte[] aKey); /** * Loads an account from a pickled base64 string.
diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java index 0cb06fa..a04286b 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java @@ -39,6 +39,13 @@ public class OlmException extends IOException { public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_SERIALIZATION = 12; public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_DESERIALIZATION = 13; + public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 20; + public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 21; + public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 22; + public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 23; + public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 24; + public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 25; + // exception human readable messages public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "createNewSession() failed"; public static final String EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION = "createNewSession() failed"; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp index d57e55b..bac8b34 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp @@ -82,16 +82,18 @@ JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz * Initialize a new account and return it to JAVA side.
* Since a C prt is returned as a jlong, special care will be taken * to make the cast (OlmAccount* => jlong) platform independent. -* @return the initialized OlmAccount* instance if init succeed, NULL otherwise +* @return the initialized OlmAccount* instance **/ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz) { + const char* errorMessage = NULL; OlmAccount *accountPtr = initializeAccountMemory(); // init account memory allocation if (!accountPtr) { LOGE("## initNewAccount(): failure - init account OOM"); + errorMessage = "init account OOM"; } else { @@ -107,6 +109,7 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi if ((0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize)) { LOGE("## initNewAccount(): failure - random buffer init"); + errorMessage = "random buffer init"; } else { @@ -116,6 +119,7 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi if (accountRetCode == olm_error()) { LOGE("## initNewAccount(): failure - account creation failed Msg=%s", olm_account_last_error(accountPtr)); + errorMessage = olm_account_last_error(accountPtr); } LOGD("## initNewAccount(): success - OLM account created"); @@ -128,6 +132,11 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi } } + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + return (jlong)(intptr_t)accountPtr; } @@ -141,12 +150,14 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi **/ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject thiz) { + const char* errorMessage = NULL; jbyteArray byteArrayRetValue = NULL; OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); if (!accountPtr) { LOGE("## identityKeys(): failure - invalid Account ptr=NULL"); + errorMessage = "invalid Account ptr"; } else { @@ -159,6 +170,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject if (!identityKeysBytesPtr) { LOGE("## identityKeys(): failure - identity keys array OOM"); + errorMessage = "identity keys array OOM"; } else { @@ -167,7 +179,8 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject if (keysResult == olm_error()) { - LOGE("## identityKeys(): failure - error getting identity keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); + errorMessage = (const char *)olm_account_last_error(accountPtr); + LOGE("## identityKeys(): failure - error getting identity keys Msg=%s", errorMessage); } else { @@ -177,6 +190,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject if (!byteArrayRetValue) { LOGE("## identityKeys(): failure - return byte array OOM"); + errorMessage = "byte array OOM"; } else { @@ -189,6 +203,11 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject } } + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + return byteArrayRetValue; } @@ -221,16 +240,16 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thi /** * Generate "one time keys". * @param aNumberOfKeys number of keys to generate - * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise **/ -JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys) +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys) { + const char* errorMessage = NULL; OlmAccount *accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); - jint retCode = ERROR_CODE_KO; if (!accountPtr) { LOGE("## generateOneTimeKeysJni(): failure - invalid Account ptr"); + errorMessage = "invalid Account ptr"; } else { @@ -243,6 +262,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject if ((0 != randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength)) { LOGE("## generateOneTimeKeysJni(): failure - random buffer init"); + errorMessage = "random buffer init"; } else { @@ -251,12 +271,13 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject // retrieve key pairs in keysBytesPtr size_t result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength); - if (result == olm_error()) { - LOGE("## generateOneTimeKeysJni(): failure - error generating one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); + if (result == olm_error()) + { + errorMessage = olm_account_last_error(accountPtr); + LOGE("## generateOneTimeKeysJni(): failure - error generating one time keys Msg=%s", errorMessage); } else { - retCode = ERROR_CODE_OK; LOGD("## generateOneTimeKeysJni(): success - result=%lu", static_cast(result)); } } @@ -268,7 +289,10 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject } } - return retCode; + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } } /** @@ -278,6 +302,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject **/ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz) { + const char* errorMessage = NULL; jbyteArray byteArrayRetValue = NULL; OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); @@ -286,6 +311,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t if (!accountPtr) { LOGE("## oneTimeKeysJni(): failure - invalid Account ptr"); + errorMessage = "invalid Account ptr"; } else { @@ -296,13 +322,16 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t if (!keysBytesPtr) { LOGE("## oneTimeKeysJni(): failure - one time keys array OOM"); + errorMessage = "one time keys array OOM"; } else { // retrieve key pairs in keysBytesPtr size_t keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); + if (keysResult == olm_error()) { LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); + errorMessage = (const char *)olm_account_last_error(accountPtr); } else { @@ -312,6 +341,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t if (!byteArrayRetValue) { LOGE("## oneTimeKeysJni(): failure - return byte array OOM"); + errorMessage = "return byte array OOM"; } else { @@ -324,6 +354,11 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t } } + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + return byteArrayRetValue; } @@ -331,21 +366,24 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject t * Remove the "one time keys" that the session used from the account. * Return the public parts of the unpublished "one time keys" for the account * @param aNativeOlmSessionId session instance - * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS if no matching keys, ERROR_CODE_KO otherwise + * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise **/ -JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId) +JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysJni)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId) { + const char* errorMessage = NULL; jint retCode = ERROR_CODE_KO; OlmAccount* accountPtr = NULL; OlmSession* sessionPtr = (OlmSession*)aNativeOlmSessionId; if (!sessionPtr) { - LOGE("## removeOneTimeKeysForSessionJni(): failure - invalid session ptr"); + LOGE("## removeOneTimeKeysJni(): failure - invalid session ptr"); + errorMessage = "invalid session ptr"; } else if(!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) { - LOGE("## removeOneTimeKeysForSessionJni(): failure - invalid account ptr"); + LOGE("## removeOneTimeKeysJni(): failure - invalid account ptr"); + errorMessage = "invalid account ptr"; } else { @@ -353,33 +391,36 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env, if (result == olm_error()) { // the account doesn't have any matching "one time keys".. - LOGW("## removeOneTimeKeysForSessionJni(): failure - removing one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); - - retCode = ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS; + LOGW("## removeOneTimeKeysJni(): failure - removing one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); + errorMessage = (const char *)olm_account_last_error(accountPtr); } else { retCode = ERROR_CODE_OK; - LOGD("## removeOneTimeKeysForSessionJni(): success"); + LOGD("## removeOneTimeKeysJni(): success"); } } + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + return retCode; } /** * Mark the current set of "one time keys" as being published. - * @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise **/ -JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz) +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz) { - jint retCode = ERROR_CODE_OK; + const char* errorMessage = NULL; OlmAccount* accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz); if (!accountPtr) { LOGE("## markOneTimeKeysAsPublishedJni(): failure - invalid account ptr"); - retCode = ERROR_CODE_KO; + errorMessage = "invalid account ptr"; } else { @@ -388,7 +429,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, if (result == olm_error()) { LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr)); - retCode = ERROR_CODE_KO; + errorMessage = (const char *)olm_account_last_error(accountPtr); } else { @@ -396,7 +437,10 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, } } - return retCode; + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } } /** @@ -407,16 +451,19 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, **/ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage) { + const char* errorMessage = NULL; OlmAccount* accountPtr = NULL; jbyteArray signedMsgRetValueBuffer = NULL; if (!aMessage) { LOGE("## signMessageJni(): failure - invalid aMessage param"); + errorMessage = "invalid aMessage param"; } else if(!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) { LOGE("## signMessageJni(): failure - invalid account ptr"); + errorMessage = "invalid account ptr"; } else { @@ -431,6 +478,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t if (!signedMsgPtr) { LOGE("## signMessageJni(): failure - signature allocation OOM"); + errorMessage = "signature allocation OOM"; } else { @@ -444,6 +492,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t if (resultSign == olm_error()) { LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr)); + errorMessage = (const char *)olm_account_last_error(accountPtr); } else { @@ -467,21 +516,23 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject t } } + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + return signedMsgRetValueBuffer; } /** * Serialize and encrypt account instance into a base64 string.
* @param aKeyBuffer key used to encrypt the serialized account data -* @param[out] aErrorMsg error message set if operation failed * @return a base64 string if operation succeed, null otherwise **/ -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg) +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer) { + const char* errorMessage = NULL; jbyteArray pickledDataRetValue = 0; - jclass errorMsgJClass = 0; - jmethodID errorMsgMethodId = 0; - jstring errorJstring = 0; jbyte* keyPtr = NULL; OlmAccount* accountPtr = NULL; @@ -490,26 +541,17 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi if (!aKeyBuffer) { LOGE(" ## serializeJni(): failure - invalid key"); - } - else if (!aErrorMsg) - { - LOGE(" ## serializeJni(): failure - invalid error object"); + errorMessage = "invalid key"; } else if (!(accountPtr = (OlmAccount*)getAccountInstanceId(env,thiz))) { LOGE(" ## serializeJni(): failure - invalid account ptr"); - } - else if (!(errorMsgJClass = env->GetObjectClass(aErrorMsg))) - { - LOGE(" ## serializeJni(): failure - unable to get error class"); - } - else if (!(errorMsgMethodId = env->GetMethodID(errorMsgJClass, "append", "(Ljava/lang/String;)Ljava/lang/StringBuffer;"))) - { - LOGE(" ## serializeJni(): failure - unable to get error method ID"); + errorMessage = "invalid account ptr"; } else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, NULL))) { LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM"); + errorMessage = "keyPtr JNI allocation OOM"; } else { @@ -523,6 +565,7 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi if (!pickledPtr) { LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM"); + errorMessage = "pickledPtr buffer OOM"; } else { @@ -533,13 +576,8 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi pickledLength); if (result == olm_error()) { - const char *errorMsgPtr = olm_account_last_error(accountPtr); - LOGE(" ## serializeJni(): failure - olm_pickle_account() Msg=%s",errorMsgPtr); - - if(0 != (errorJstring = env->NewStringUTF(errorMsgPtr))) - { - env->CallObjectMethod(aErrorMsg, errorMsgMethodId, errorJstring); - } + errorMessage = olm_account_last_error(accountPtr); + LOGE(" ## serializeJni(): failure - olm_pickle_account() Msg=%s", errorMessage); } else { @@ -562,6 +600,11 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thi env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); } + if (errorMessage) + { + env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); + } + return pickledDataRetValue; } diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h index 94a0381..5b73acd 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h @@ -39,15 +39,15 @@ JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject // one time keys JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz); JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys); -JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId); -JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz); +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys); +JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysJni)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId); +JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz); // signing JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage); // serialization -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer, jobject aErrorMsg); +JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer); JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer); #ifdef __cplusplus diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h index f1d124e..1c80388 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h +++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h @@ -55,7 +55,6 @@ namespace AndroidOlmSdk { // Error codes definition static const int ERROR_CODE_OK = 0; - static const int ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS = ERROR_CODE_OK+1; static const int ERROR_CODE_KO = -1; // constants -- cgit v1.2.3