Commit f257580c authored by ylecollen's avatar ylecollen
Browse files

verifyEd25519Signature triggers an exception instead of filling an error buffer

parent 03396102
...@@ -58,7 +58,7 @@ public class OlmUtilityTest { ...@@ -58,7 +58,7 @@ public class OlmUtilityTest {
@Test @Test
public void test01VerifyEd25519Signing() { public void test01VerifyEd25519Signing() {
String fingerPrintKey = null; 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\"}"; 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; OlmAccount account = null;
...@@ -98,22 +98,43 @@ public class OlmUtilityTest { ...@@ -98,22 +98,43 @@ public class OlmUtilityTest {
OlmUtility utility = new OlmUtility(); OlmUtility utility = new OlmUtility();
// verify signature // verify signature
errorMsg.append("init with anything"); boolean isVerified;
boolean isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message, errorMsg);
isVerified = false;
errorMsg = null;
try {
isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message);
} catch (Exception e) {
errorMsg = e.getMessage();
}
assertTrue(isVerified); assertTrue(isVerified);
assertTrue(String.valueOf(errorMsg).isEmpty()); assertTrue(TextUtils.isEmpty(errorMsg));
// check a bad signature is detected => errorMsg = BAD_MESSAGE_MAC // check a bad signature is detected => errorMsg = BAD_MESSAGE_MAC
String badSignature = "Bad signature Bad signature Bad signature.."; 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(isVerified);
assertFalse(String.valueOf(errorMsg).isEmpty()); assertTrue(!TextUtils.isEmpty(errorMsg));
// check bad fingerprint size => errorMsg = INVALID_BASE64 // check bad fingerprint size => errorMsg = INVALID_BASE64
String badSizeFingerPrintKey = fingerPrintKey.substring(fingerPrintKey.length()/2); 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(isVerified);
assertFalse(String.valueOf(errorMsg).isEmpty()); assertTrue(!TextUtils.isEmpty(errorMsg));
utility.releaseUtility(); utility.releaseUtility();
assertTrue(utility.isReleased()); assertTrue(utility.isReleased());
......
...@@ -27,33 +27,35 @@ public class OlmException extends IOException { ...@@ -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_INIT_ACCOUNT_CREATION = 10;
public static final int EXCEPTION_CODE_ACCOUNT_SERIALIZATION = 20; public static final int EXCEPTION_CODE_ACCOUNT_SERIALIZATION = 100;
public static final int EXCEPTION_CODE_ACCOUNT_DESERIALIZATION = 21; public static final int EXCEPTION_CODE_ACCOUNT_DESERIALIZATION = 101;
public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 22; public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 102;
public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 23; public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 103;
public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 24; public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 104;
public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 25; public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 105;
public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 26; public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 106;
public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 27; 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_CREATE_INBOUND_GROUP_SESSION = 200;
public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 31; public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 201;
public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER = 32; public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER = 202;
public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_DECRYPT_SESSION = 33; 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_CREATE_OUTBOUND_GROUP_SESSION = 300;
public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 41; public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 301;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER = 42; public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER = 302;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY = 43; public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY = 303;
public static final int EXCEPTION_CODE_OUTBOUND_GROUP_ENCRYPT_MESSAGE = 44; 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_INIT_SESSION_CREATION = 400;
public static final int EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION = 51; public static final int EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION = 401;
public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION = 52; public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION = 402;
public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM = 53; public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM = 403;
public static final int EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE = 54; public static final int EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE = 404;
public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 55; public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 405;
public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 56; public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 406;
public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500;
// exception human readable messages // exception human readable messages
public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "createNewSession() failed"; public static final String EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION = "createNewSession() failed";
......
...@@ -70,33 +70,28 @@ public class OlmUtility { ...@@ -70,33 +70,28 @@ public class OlmUtility {
* @param aSignature the base64-encoded message signature to be checked. * @param aSignature the base64-encoded message signature to be checked.
* @param aFingerprintKey the ed25519 key (fingerprint key) * @param aFingerprintKey the ed25519 key (fingerprint key)
* @param aMessage the signed message * @param aMessage the signed message
* @param aError error message description
* @return true if the signature is verified, false otherwise * @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) { public boolean verifyEd25519Signature(String aSignature, String aFingerprintKey, String aMessage) throws OlmException {
boolean retCode = false; String errorMessage = null;
String jniError;
try {
if (null == aError) { if (TextUtils.isEmpty(aSignature) || TextUtils.isEmpty(aFingerprintKey) || TextUtils.isEmpty(aMessage)) {
Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input error parameter"); Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input parameters");
} else { errorMessage = "JAVA sanity check failure - invalid input parameters";
aError.setLength(0); } else {
errorMessage = verifyEd25519SignatureJni(aSignature.getBytes("UTF-8"), aFingerprintKey.getBytes("UTF-8"), aMessage.getBytes("UTF-8"));
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());
} }
} 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;
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment