From 1b3ea01fc9b25e638cd38248b088b818807baed4 Mon Sep 17 00:00:00 2001 From: Shams Asari Date: Wed, 6 Dec 2023 09:46:53 +0000 Subject: [PATCH] ENT-11112: Enabled X509EdDSAEngineTest (#7595) --- core/build.gradle | 20 ++++- .../core/internal/X509EdDSAEngineTest.java | 77 ++++++++++--------- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index e6ace1ed78..9916f83039 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -83,20 +83,20 @@ dependencies { // JDK11: required by Quasar at run-time testRuntimeOnly "com.esotericsoftware:kryo:$kryo_version" + testRuntimeOnly "org.slf4j:slf4j-simple:$slf4j_version" testImplementation "org.mockito.kotlin:mockito-kotlin:$mockito_kotlin_version" testImplementation "org.mockito:mockito-core:$mockito_version" testImplementation "org.assertj:assertj-core:$assertj_version" testImplementation "com.natpryce:hamkrest:$hamkrest_version" testImplementation 'org.hamcrest:hamcrest-library:2.1' - } // TODO Consider moving it to quasar-utils in the future (introduced with PR-1388) -task copyQuasarJar(type: Copy) { +tasks.register('copyQuasarJar', Copy) { from configurations.quasar into "$project.rootProject.projectDir/lib" - rename { filename -> "quasar.jar"} + rename { filename -> "quasar.jar" } } jar { @@ -122,11 +122,23 @@ processTestResources { } } +compileTestJava { + options.compilerArgs += [ + '--add-exports', 'java.base/sun.security.util=ALL-UNNAMED', + '--add-exports', 'java.base/sun.security.x509=ALL-UNNAMED' + ] +} + test { + jvmArgs += [ + '--add-exports', 'java.base/sun.security.util=ALL-UNNAMED', + '--add-exports', 'java.base/sun.security.x509=ALL-UNNAMED' + ] maxParallelForks = (System.env.CORDA_CORE_TESTING_FORKS == null) ? 1 : "$System.env.CORDA_CORE_TESTING_FORKS".toInteger() } -task testJar(type: Jar, dependsOn: testClasses) { +tasks.register('testJar', Jar) { + dependsOn testClasses classifier "tests" from sourceSets.test.output } diff --git a/core/src/test/java/net/corda/core/internal/X509EdDSAEngineTest.java b/core/src/test/java/net/corda/core/internal/X509EdDSAEngineTest.java index 39b825eeb8..e353db8043 100644 --- a/core/src/test/java/net/corda/core/internal/X509EdDSAEngineTest.java +++ b/core/src/test/java/net/corda/core/internal/X509EdDSAEngineTest.java @@ -3,8 +3,11 @@ package net.corda.core.internal; import net.corda.core.crypto.Crypto; import net.i2p.crypto.eddsa.EdDSAEngine; import net.i2p.crypto.eddsa.EdDSAPublicKey; -import org.junit.Ignore; import org.junit.Test; +import sun.security.util.BitArray; +import sun.security.util.ObjectIdentifier; +import sun.security.x509.AlgorithmId; +import sun.security.x509.X509Key; import java.io.IOException; import java.math.BigInteger; @@ -13,6 +16,7 @@ import java.security.KeyPair; import java.security.SignatureException; import java.util.Random; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.junit.Assert.assertTrue; /** @@ -23,43 +27,41 @@ import static org.junit.Assert.assertTrue; * import sun.security.x509.X509Key; */ public class X509EdDSAEngineTest { - - private static long SEED = 20170920L; - private static int TEST_DATA_SIZE = 2000; + private static final long SEED = 20170920L; + private static final int TEST_DATA_SIZE = 2000; // offset into an EdDSA header indicating where the key header and actual key start // in the underlying byte array - private static int keyHeaderStart = 9; - private static int keyStart = 12; + private static final int KEY_HEADER_START = 9; + private static final int KEY_START = 12; -// private X509Key toX509Key(EdDSAPublicKey publicKey) throws IOException, InvalidKeyException { -// byte[] internals = publicKey.getEncoded(); -// -// // key size in the header includes the count unused bits at the end of the key -// // [keyHeaderStart + 2] but NOT the key header ID [keyHeaderStart] so the -// // actual length of the key blob is size - 1 -// int keySize = (internals[keyHeaderStart + 1]) - 1; -// -// byte[] key = new byte[keySize]; -// System.arraycopy(internals, keyStart, key, 0, keySize); -// -// // 1.3.101.102 is the EdDSA OID -// return new TestX509Key(new AlgorithmId(new ObjectIdentifier(new DerInputStream("1.3.101.112".getBytes(StandardCharsets.UTF_8)))), new BitArray(keySize * 8, key)); -// } + private X509Key toX509Key(EdDSAPublicKey publicKey) throws IOException, InvalidKeyException { + byte[] internals = publicKey.getEncoded(); -// class TestX509Key extends X509Key { -// TestX509Key(AlgorithmId algorithmId, BitArray key) throws InvalidKeyException { -// this.algid = algorithmId; -// this.setKey(key); -// this.encode(); -// } -// } + // key size in the header includes the count unused bits at the end of the key + // [keyHeaderStart + 2] but NOT the key header ID [keyHeaderStart] so the + // actual length of the key blob is size - 1 + int keySize = (internals[KEY_HEADER_START + 1]) - 1; + + byte[] key = new byte[keySize]; + System.arraycopy(internals, KEY_START, key, 0, keySize); + + // 1.3.101.102 is the EdDSA OID + return new TestX509Key(new AlgorithmId(ObjectIdentifier.of("1.3.101.112")), new BitArray(keySize * 8, key)); + } + + private static class TestX509Key extends X509Key { + TestX509Key(AlgorithmId algorithmId, BitArray key) throws InvalidKeyException { + this.algid = algorithmId; + this.setKey(key); + this.encode(); + } + } /** * Put the X509EdDSA engine through basic tests to verify that the functions are hooked up correctly. */ @Test - @Ignore("TODO JDK17:Fixme") public void SignAndVerify() throws InvalidKeyException, SignatureException { X509EdDSAEngine engine = new X509EdDSAEngine(); KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.EDDSA_ED25519_SHA512, BigInteger.valueOf(SEED)); @@ -82,11 +84,10 @@ public class X509EdDSAEngineTest { * Verify that signing with an X509Key wrapped EdDSA key works. */ @Test - @Ignore public void SignAndVerifyWithX509Key() throws InvalidKeyException, SignatureException, IOException { X509EdDSAEngine engine = new X509EdDSAEngine(); KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.EDDSA_ED25519_SHA512, BigInteger.valueOf(SEED + 1)); -// X509Key publicKey = toX509Key((EdDSAPublicKey) keyPair.getPublic()); + X509Key publicKey = toX509Key((EdDSAPublicKey) keyPair.getPublic()); byte[] randomBytes = new byte[TEST_DATA_SIZE]; new Random(SEED + 1).nextBytes(randomBytes); engine.initSign(keyPair.getPrivate()); @@ -96,7 +97,7 @@ public class X509EdDSAEngineTest { // Now verify the signature byte[] signature = engine.sign(); -// engine.initVerify(publicKey); + engine.initVerify(publicKey); engine.update(randomBytes); assertTrue(engine.verify(signature)); } @@ -105,11 +106,10 @@ public class X509EdDSAEngineTest { * Verify that signing with an X509Key wrapped EdDSA key succeeds when using the underlying EdDSAEngine. */ @Test - @Ignore public void SignAndVerifyWithX509KeyAndOldEngineFails() throws InvalidKeyException, SignatureException, IOException { X509EdDSAEngine engine = new X509EdDSAEngine(); KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.EDDSA_ED25519_SHA512, BigInteger.valueOf(SEED + 1)); -// X509Key publicKey = toX509Key((EdDSAPublicKey) keyPair.getPublic()); + X509Key publicKey = toX509Key((EdDSAPublicKey) keyPair.getPublic()); byte[] randomBytes = new byte[TEST_DATA_SIZE]; new Random(SEED + 1).nextBytes(randomBytes); engine.initSign(keyPair.getPrivate()); @@ -118,17 +118,18 @@ public class X509EdDSAEngineTest { // Now verify the signature byte[] signature = engine.sign(); -// engine.initVerify(publicKey); + engine.initVerify(publicKey); engine.update(randomBytes); engine.verify(signature); } /** Verify will fail if the input public key cannot be converted to EdDSA public key. */ - @Test(expected = InvalidKeyException.class) - @Ignore - public void verifyWithNonSupportedKeyTypeFails() throws InvalidKeyException { + @Test + public void verifyWithNonSupportedKeyTypeFails() { EdDSAEngine engine = new EdDSAEngine(); KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.ECDSA_SECP256K1_SHA256, BigInteger.valueOf(SEED)); - engine.initVerify(keyPair.getPublic()); + assertThatExceptionOfType(InvalidKeyException.class).isThrownBy(() -> + engine.initVerify(keyPair.getPublic()) + ); } }