Fix verifyEd25519Signature() API to properly return the error message in case signature verification failed
- update unit tests
This commit is contained in:
parent
a6913c49c2
commit
26d9934fc8
2 changed files with 29 additions and 10 deletions
|
@ -45,7 +45,7 @@ public class OlmUtilityTest {
|
||||||
@Test
|
@Test
|
||||||
public void test01VerifyEd25519Signing() {
|
public void test01VerifyEd25519Signing() {
|
||||||
String fingerPrintKey = null;
|
String fingerPrintKey = null;
|
||||||
String errorMsg = new String();
|
StringBuffer errorMsg = new StringBuffer();
|
||||||
String message = "{\"key1\":\"value1\",\"key2\":\"value2\"};";
|
String message = "{\"key1\":\"value1\",\"key2\":\"value2\"};";
|
||||||
|
|
||||||
// create account
|
// create account
|
||||||
|
@ -67,10 +67,20 @@ public class OlmUtilityTest {
|
||||||
assertTrue("Exception MSg="+e.getMessage(), false);
|
assertTrue("Exception MSg="+e.getMessage(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// instance utility
|
// instantiate utility object
|
||||||
OlmUtility utility = new OlmUtility();
|
OlmUtility utility = new OlmUtility();
|
||||||
|
|
||||||
|
// verify signature
|
||||||
|
errorMsg.append("init with anything");
|
||||||
boolean isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message, errorMsg);
|
boolean isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message, errorMsg);
|
||||||
assertTrue(isVerified);
|
assertTrue(isVerified);
|
||||||
|
assertTrue(String.valueOf(errorMsg).isEmpty());
|
||||||
|
|
||||||
|
// check a bad signature is detected and the error message is not empty
|
||||||
|
messageSignature = "Bad signature Bad signature Bad signature..";
|
||||||
|
isVerified = utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message, errorMsg);
|
||||||
|
assertFalse(isVerified);
|
||||||
|
assertFalse(String.valueOf(errorMsg).isEmpty());
|
||||||
|
|
||||||
utility.releaseUtility();
|
utility.releaseUtility();
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,14 +71,14 @@ public class OlmUtility implements Serializable {
|
||||||
* If the key was too small, aError is set to "OLM.INVALID_BASE64".
|
* If the key was too small, aError is set to "OLM.INVALID_BASE64".
|
||||||
* If the signature was invalid, aError is set to "OLM.BAD_MESSAGE_MAC".<br>
|
* If the signature was invalid, aError is set to "OLM.BAD_MESSAGE_MAC".<br>
|
||||||
* @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
|
* @param aFingerprintKey the ed25519 key (fingerprint key)
|
||||||
* @param aMessage the message which was signed
|
* @param aMessage the signed message
|
||||||
* @param aError error message description
|
* @param aError error message description
|
||||||
* @return true if the signature is verified, false otherwise
|
* @return true if the signature is verified, false otherwise
|
||||||
*/
|
*/
|
||||||
public boolean verifyEd25519Signature(String aSignature, String aFingerprintKey, String aMessage, String aError) {
|
public boolean verifyEd25519Signature(String aSignature, String aFingerprintKey, String aMessage, StringBuffer aError) {
|
||||||
boolean retCode = false;
|
boolean retCode = false;
|
||||||
OlmUtility retObj=null;
|
String errorRetValue = null;
|
||||||
|
|
||||||
if(null == aError) {
|
if(null == aError) {
|
||||||
Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input error parameter");
|
Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input error parameter");
|
||||||
|
@ -86,17 +86,26 @@ public class OlmUtility implements Serializable {
|
||||||
else if(TextUtils.isEmpty(aSignature) || TextUtils.isEmpty(aFingerprintKey) || TextUtils.isEmpty(aMessage)){
|
else if(TextUtils.isEmpty(aSignature) || TextUtils.isEmpty(aFingerprintKey) || TextUtils.isEmpty(aMessage)){
|
||||||
Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input parameters");
|
Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input parameters");
|
||||||
} else {
|
} else {
|
||||||
String errorRetValue = verifyEd25519SignatureJni(aSignature,aFingerprintKey, aMessage);
|
aError.setLength(0);
|
||||||
if(null == errorRetValue) {
|
|
||||||
aError="";
|
if( null == (errorRetValue = verifyEd25519SignatureJni(aSignature,aFingerprintKey, aMessage))) {
|
||||||
retCode = true;
|
retCode = true;
|
||||||
} else {
|
} else {
|
||||||
aError = errorRetValue;
|
aError.append(errorRetValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return retCode;
|
return retCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify an ed25519 signature.
|
||||||
|
* Return a human readable error message in case of verification failure.
|
||||||
|
* @param aSignature the base64-encoded message signature to be checked.
|
||||||
|
* @param aFingerprintKey the ed25519 key
|
||||||
|
* @param aMessage the signed message
|
||||||
|
* @return null if validation succeed, the error message string if operation failed
|
||||||
|
*/
|
||||||
private native String verifyEd25519SignatureJni(String aSignature, String aFingerprintKey, String aMessage);
|
private native String verifyEd25519SignatureJni(String aSignature, String aFingerprintKey, String aMessage);
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue