From f257580cb1adb798943b2b9bc54192320c2821e2 Mon Sep 17 00:00:00 2001 From: ylecollen Date: Mon, 9 Jan 2017 10:25:18 +0100 Subject: verifyEd25519Signature triggers an exception instead of filling an error buffer --- .../java/org/matrix/olm/OlmUtilityTest.java | 37 ++++++++++++---- .../src/main/java/org/matrix/olm/OlmException.java | 50 +++++++++++----------- .../src/main/java/org/matrix/olm/OlmUtility.java | 39 ++++++++--------- 3 files changed, 72 insertions(+), 54 deletions(-) (limited to 'java/android/OlmLibSdk') diff --git a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java index 4096001..bd53e6e 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java +++ b/java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java @@ -58,7 +58,7 @@ public class OlmUtilityTest { @Test public void test01VerifyEd25519Signing() { String fingerPrintKey = null; - StringBuffer errorMsg = new StringBuffer(); + String errorMsg = null; String message = "{\"algorithms\":[\"m.megolm.v1.aes-sha2\",\"m.olm.v1.curve25519-aes-sha2\"],\"device_id\":\"YMBYCWTWCG\",\"keys\":{\"curve25519:YMBYCWTWCG\":\"KZFa5YUXV2EOdhK8dcGMMHWB67stdgAP4+xwiS69mCU\",\"ed25519:YMBYCWTWCG\":\"0cEgQJJqjgtXUGp4ZXQQmh36RAxwxr8HJw2E9v1gvA0\"},\"user_id\":\"@mxBob14774891254276b253f42-f267-43ec-bad9-767142bfea30:localhost:8480\"}"; OlmAccount account = null; @@ -98,22 +98,43 @@ public class OlmUtilityTest { OlmUtility utility = new OlmUtility(); // verify signature - errorMsg.append("init with anything"); - boolean isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message, errorMsg); + boolean isVerified; + + isVerified = false; + errorMsg = null; + try { + isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message); + } catch (Exception e) { + errorMsg = e.getMessage(); + } assertTrue(isVerified); - assertTrue(String.valueOf(errorMsg).isEmpty()); + assertTrue(TextUtils.isEmpty(errorMsg)); // check a bad signature is detected => errorMsg = BAD_MESSAGE_MAC String badSignature = "Bad signature Bad signature Bad signature.."; - isVerified = utility.verifyEd25519Signature(badSignature, fingerPrintKey, message, errorMsg); + + isVerified = false; + errorMsg = null; + try { + isVerified = utility.verifyEd25519Signature(badSignature, fingerPrintKey, message); + } catch (Exception e) { + errorMsg = e.getMessage(); + } assertFalse(isVerified); - assertFalse(String.valueOf(errorMsg).isEmpty()); + assertTrue(!TextUtils.isEmpty(errorMsg)); // check bad fingerprint size => errorMsg = INVALID_BASE64 String badSizeFingerPrintKey = fingerPrintKey.substring(fingerPrintKey.length()/2); - isVerified = utility.verifyEd25519Signature(messageSignature, badSizeFingerPrintKey, message, errorMsg); + + isVerified = false; + errorMsg = null; + try { + isVerified = utility.verifyEd25519Signature(messageSignature, badSizeFingerPrintKey, message); + } catch (Exception e) { + errorMsg = e.getMessage(); + } assertFalse(isVerified); - assertFalse(String.valueOf(errorMsg).isEmpty()); + assertTrue(!TextUtils.isEmpty(errorMsg)); utility.releaseUtility(); assertTrue(utility.isReleased()); 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 a6f1805..d02b7fc 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 @@ -27,33 +27,35 @@ public class OlmException extends IOException { public static final int EXCEPTION_CODE_INIT_ACCOUNT_CREATION = 10; - public static final int EXCEPTION_CODE_ACCOUNT_SERIALIZATION = 20; - public static final int EXCEPTION_CODE_ACCOUNT_DESERIALIZATION = 21; - public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 22; - public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 23; - public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 24; - public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 25; - public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 26; - public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 27; + public static final int EXCEPTION_CODE_ACCOUNT_SERIALIZATION = 100; + public static final int EXCEPTION_CODE_ACCOUNT_DESERIALIZATION = 101; + public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 102; + public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 103; + public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 104; + public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 105; + public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 106; + public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 107; - public static final int EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION = 30; - public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 31; - public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER = 32; - public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_DECRYPT_SESSION = 33; + public static final int EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION = 200; + public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 201; + public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER = 202; + public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_DECRYPT_SESSION = 203; - public static final int EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION = 40; - public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 41; - public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER = 42; - public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY = 43; - public static final int EXCEPTION_CODE_OUTBOUND_GROUP_ENCRYPT_MESSAGE = 44; + public static final int EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION = 300; + public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 301; + public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER = 302; + public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY = 303; + public static final int EXCEPTION_CODE_OUTBOUND_GROUP_ENCRYPT_MESSAGE = 304; - public static final int EXCEPTION_CODE_INIT_SESSION_CREATION = 50; - public static final int EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION = 51; - public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION = 52; - public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM = 53; - public static final int EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE = 54; - public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 55; - public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 56; + public static final int EXCEPTION_CODE_INIT_SESSION_CREATION = 400; + public static final int EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION = 401; + public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION = 402; + public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM = 403; + public static final int EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE = 404; + public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 405; + public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 406; + + public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500; // exception human readable messages public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "createNewSession() failed"; diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java index 28d8b58..3b1d9e1 100644 --- a/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java +++ b/java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java @@ -70,33 +70,28 @@ public class OlmUtility { * @param aSignature the base64-encoded message signature to be checked. * @param aFingerprintKey the ed25519 key (fingerprint key) * @param aMessage the signed message - * @param aError error message description * @return true if the signature is verified, false otherwise + * @exception OlmException the failure reason */ - public boolean verifyEd25519Signature(String aSignature, String aFingerprintKey, String aMessage, StringBuffer aError) { - boolean retCode = false; - String jniError; - - if (null == aError) { - Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input error parameter"); - } else { - aError.setLength(0); - - try { - if (TextUtils.isEmpty(aSignature) || TextUtils.isEmpty(aFingerprintKey) || TextUtils.isEmpty(aMessage)) { - Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input parameters"); - aError.append("JAVA sanity check failure - invalid input parameters"); - } else if (null == (jniError = verifyEd25519SignatureJni(aSignature.getBytes("UTF-8"), aFingerprintKey.getBytes("UTF-8"), aMessage.getBytes("UTF-8")))) { - retCode = true; - } else { - aError.append(jniError); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## verifyEd25519Signature(): failed " + e.getMessage()); + public boolean verifyEd25519Signature(String aSignature, String aFingerprintKey, String aMessage) throws OlmException { + String errorMessage = null; + + try { + if (TextUtils.isEmpty(aSignature) || TextUtils.isEmpty(aFingerprintKey) || TextUtils.isEmpty(aMessage)) { + Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input parameters"); + errorMessage = "JAVA sanity check failure - invalid input parameters"; + } else { + errorMessage = verifyEd25519SignatureJni(aSignature.getBytes("UTF-8"), aFingerprintKey.getBytes("UTF-8"), aMessage.getBytes("UTF-8")); } + } catch (Exception e) { + Log.e(LOG_TAG, "## verifyEd25519Signature(): failed " + e.getMessage()); } - return retCode; + if (!TextUtils.isEmpty(errorMessage)) { + throw new OlmException(OlmException.EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE, errorMessage); + } + + return true; } /** -- cgit v1.2.3