F-3995: throw on operational errors in RsaPSS VerifyCheck wrapper
parent
17818e0f19
commit
83ffe17d83
|
|
@ -1725,6 +1725,10 @@ Java_com_wolfssl_wolfcrypt_Rsa_wc_1RsaPSS_1VerifyCheck(
|
|||
}
|
||||
}
|
||||
|
||||
if (ret < 0) {
|
||||
throwWolfCryptExceptionFromError(env, ret);
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
XMEMSET(output, 0, outputSz);
|
||||
|
||||
|
|
@ -1737,6 +1741,11 @@ Java_com_wolfssl_wolfcrypt_Rsa_wc_1RsaPSS_1VerifyCheck(
|
|||
if (ret > 0) {
|
||||
result = JNI_TRUE;
|
||||
}
|
||||
/* PSS decode of a bad signature fails with data dependent
|
||||
* error codes, all map to false, only MEMORY_E throws */
|
||||
else if (ret == MEMORY_E) {
|
||||
throwWolfCryptExceptionFromError(env, ret);
|
||||
}
|
||||
}
|
||||
else {
|
||||
/* Non-default salt length */
|
||||
|
|
@ -1749,6 +1758,12 @@ Java_com_wolfssl_wolfcrypt_Rsa_wc_1RsaPSS_1VerifyCheck(
|
|||
if (ret == 0) {
|
||||
result = JNI_TRUE;
|
||||
}
|
||||
else if (ret == MEMORY_E) {
|
||||
throwWolfCryptExceptionFromError(env, ret);
|
||||
}
|
||||
}
|
||||
else if (ret == MEMORY_E) {
|
||||
throwWolfCryptExceptionFromError(env, ret);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -921,9 +921,11 @@ public class Rsa extends NativeStruct {
|
|||
* @param mgf mask generation function (WC_MGF1SHA256 for MGF1 with SHA-256)
|
||||
* @param saltLen salt length in bytes, or special value
|
||||
*
|
||||
* @return true if signature is valid, false otherwise
|
||||
* @return true if signature is valid, false if the signature does not
|
||||
* verify against the provided digest
|
||||
*
|
||||
* @throws WolfCryptException if native operation fails
|
||||
* @throws WolfCryptException on invalid arguments or memory
|
||||
* allocation failure
|
||||
* @throws IllegalStateException if public key has not been set, if object
|
||||
* fails to initialize, or if releaseNativeStruct() has been
|
||||
* called and object has been released.
|
||||
|
|
|
|||
|
|
@ -1379,6 +1379,44 @@ public class RsaTest {
|
|||
assertTrue("RSA-PSS check verification failed with " +
|
||||
"RSA_PSS_SALT_LEN_DEFAULT", verified);
|
||||
|
||||
/* Operational errors must throw, not report false */
|
||||
try {
|
||||
key.rsaPssVerifyWithDigest(null, message, digest,
|
||||
WolfCrypt.WC_HASH_TYPE_SHA256, Rsa.WC_MGF1SHA256, 32);
|
||||
fail("rsaPssVerifyWithDigest should have thrown for " +
|
||||
"null signature");
|
||||
} catch (WolfCryptException e) {
|
||||
/* expected */
|
||||
}
|
||||
|
||||
/* Corrupted signatures must report false, not throw. PSS
|
||||
* decode of garbage fails with position dependent error
|
||||
* codes, exercise first, middle and last bytes */
|
||||
signature = key.rsaPssSign(digest,
|
||||
WolfCrypt.WC_HASH_TYPE_SHA256, Rsa.WC_MGF1SHA256, 32, rng);
|
||||
int[] flipPos = new int[] { 0, signature.length / 2,
|
||||
signature.length - 1 };
|
||||
for (int pos : flipPos) {
|
||||
byte[] badSig = signature.clone();
|
||||
badSig[pos] ^= (byte)0x80;
|
||||
verified = key.rsaPssVerifyWithDigest(badSig, message,
|
||||
digest, WolfCrypt.WC_HASH_TYPE_SHA256,
|
||||
Rsa.WC_MGF1SHA256, 32);
|
||||
assertFalse("corrupted RSA-PSS signature verified, " +
|
||||
"flipped byte " + pos, verified);
|
||||
}
|
||||
|
||||
/* Mismatched digest must report false, not throw */
|
||||
signature = key.rsaPssSign(digest,
|
||||
WolfCrypt.WC_HASH_TYPE_SHA256, Rsa.WC_MGF1SHA256, 32, rng);
|
||||
byte[] wrongDigest = digest.clone();
|
||||
wrongDigest[0] ^= 1;
|
||||
verified = key.rsaPssVerifyWithDigest(signature, message,
|
||||
wrongDigest, WolfCrypt.WC_HASH_TYPE_SHA256,
|
||||
Rsa.WC_MGF1SHA256, 32);
|
||||
assertFalse("RSA-PSS signature verified with wrong digest",
|
||||
verified);
|
||||
|
||||
} catch (Exception e) {
|
||||
fail("RSA-PSS check verification test failed: " + e.getMessage());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue