CORDA-1009 Remove X509EdDSAEngine dependency on X509Key (#2506)

This commit is contained in:
Konstantinos Chalkias 2018-02-12 16:13:04 +00:00 committed by GitHub
parent 1487c411b4
commit fa4b5d16ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -27,12 +27,11 @@ class X509EdDSAEngine : Signature {
override fun engineInitSign(privateKey: PrivateKey, random: SecureRandom) = engine.initSign(privateKey, random)
override fun engineInitVerify(publicKey: PublicKey) {
val parsedKey = if (publicKey is sun.security.x509.X509Key) {
EdDSAPublicKey(X509EncodedKeySpec(publicKey.encoded))
} else {
publicKey
val parsedKey = try {
publicKey as? EdDSAPublicKey ?: EdDSAPublicKey(X509EncodedKeySpec(publicKey.encoded))
} catch(e: Exception) {
throw (InvalidKeyException(e.message))
}
engine.initVerify(parsedKey)
}

View File

@ -114,4 +114,12 @@ class X509EdDSAEngineTest {
engine.verify(signature)
}
}
/** Verify will fail if the input public key cannot be converted to EdDSA public key. */
@Test
fun `verify with non-supported key type fails`() {
val engine = EdDSAEngine()
val keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.ECDSA_SECP256K1_SHA256, BigInteger.valueOf(SEED))
assertFailsWith<InvalidKeyException> { engine.initVerify(keyPair.public) }
}
}