ENT-11112: Enabled X509EdDSAEngineTest (#7595)

This commit is contained in:
Shams Asari 2023-12-06 09:46:53 +00:00 committed by GitHub
parent 4cf5fe55dd
commit 1b3ea01fc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 42 deletions

View File

@ -83,20 +83,20 @@ dependencies {
// JDK11: required by Quasar at run-time // JDK11: required by Quasar at run-time
testRuntimeOnly "com.esotericsoftware:kryo:$kryo_version" 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.kotlin:mockito-kotlin:$mockito_kotlin_version"
testImplementation "org.mockito:mockito-core:$mockito_version" testImplementation "org.mockito:mockito-core:$mockito_version"
testImplementation "org.assertj:assertj-core:$assertj_version" testImplementation "org.assertj:assertj-core:$assertj_version"
testImplementation "com.natpryce:hamkrest:$hamkrest_version" testImplementation "com.natpryce:hamkrest:$hamkrest_version"
testImplementation 'org.hamcrest:hamcrest-library:2.1' testImplementation 'org.hamcrest:hamcrest-library:2.1'
} }
// TODO Consider moving it to quasar-utils in the future (introduced with PR-1388) // 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 from configurations.quasar
into "$project.rootProject.projectDir/lib" into "$project.rootProject.projectDir/lib"
rename { filename -> "quasar.jar"} rename { filename -> "quasar.jar" }
} }
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 { 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() 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" classifier "tests"
from sourceSets.test.output from sourceSets.test.output
} }

View File

@ -3,8 +3,11 @@ package net.corda.core.internal;
import net.corda.core.crypto.Crypto; import net.corda.core.crypto.Crypto;
import net.i2p.crypto.eddsa.EdDSAEngine; import net.i2p.crypto.eddsa.EdDSAEngine;
import net.i2p.crypto.eddsa.EdDSAPublicKey; import net.i2p.crypto.eddsa.EdDSAPublicKey;
import org.junit.Ignore;
import org.junit.Test; 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.io.IOException;
import java.math.BigInteger; import java.math.BigInteger;
@ -13,6 +16,7 @@ import java.security.KeyPair;
import java.security.SignatureException; import java.security.SignatureException;
import java.util.Random; import java.util.Random;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/** /**
@ -23,43 +27,41 @@ import static org.junit.Assert.assertTrue;
* import sun.security.x509.X509Key; * import sun.security.x509.X509Key;
*/ */
public class X509EdDSAEngineTest { public class X509EdDSAEngineTest {
private static final long SEED = 20170920L;
private static long SEED = 20170920L; private static final int TEST_DATA_SIZE = 2000;
private static int TEST_DATA_SIZE = 2000;
// offset into an EdDSA header indicating where the key header and actual key start // offset into an EdDSA header indicating where the key header and actual key start
// in the underlying byte array // in the underlying byte array
private static int keyHeaderStart = 9; private static final int KEY_HEADER_START = 9;
private static int keyStart = 12; private static final int KEY_START = 12;
// private X509Key toX509Key(EdDSAPublicKey publicKey) throws IOException, InvalidKeyException { private X509Key toX509Key(EdDSAPublicKey publicKey) throws IOException, InvalidKeyException {
// byte[] internals = publicKey.getEncoded(); 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));
// }
// class TestX509Key extends X509Key { // key size in the header includes the count unused bits at the end of the key
// TestX509Key(AlgorithmId algorithmId, BitArray key) throws InvalidKeyException { // [keyHeaderStart + 2] but NOT the key header ID [keyHeaderStart] so the
// this.algid = algorithmId; // actual length of the key blob is size - 1
// this.setKey(key); int keySize = (internals[KEY_HEADER_START + 1]) - 1;
// this.encode();
// } 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. * Put the X509EdDSA engine through basic tests to verify that the functions are hooked up correctly.
*/ */
@Test @Test
@Ignore("TODO JDK17:Fixme")
public void SignAndVerify() throws InvalidKeyException, SignatureException { public void SignAndVerify() throws InvalidKeyException, SignatureException {
X509EdDSAEngine engine = new X509EdDSAEngine(); X509EdDSAEngine engine = new X509EdDSAEngine();
KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.EDDSA_ED25519_SHA512, BigInteger.valueOf(SEED)); 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. * Verify that signing with an X509Key wrapped EdDSA key works.
*/ */
@Test @Test
@Ignore
public void SignAndVerifyWithX509Key() throws InvalidKeyException, SignatureException, IOException { public void SignAndVerifyWithX509Key() throws InvalidKeyException, SignatureException, IOException {
X509EdDSAEngine engine = new X509EdDSAEngine(); X509EdDSAEngine engine = new X509EdDSAEngine();
KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.EDDSA_ED25519_SHA512, BigInteger.valueOf(SEED + 1)); 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]; byte[] randomBytes = new byte[TEST_DATA_SIZE];
new Random(SEED + 1).nextBytes(randomBytes); new Random(SEED + 1).nextBytes(randomBytes);
engine.initSign(keyPair.getPrivate()); engine.initSign(keyPair.getPrivate());
@ -96,7 +97,7 @@ public class X509EdDSAEngineTest {
// Now verify the signature // Now verify the signature
byte[] signature = engine.sign(); byte[] signature = engine.sign();
// engine.initVerify(publicKey); engine.initVerify(publicKey);
engine.update(randomBytes); engine.update(randomBytes);
assertTrue(engine.verify(signature)); 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. * Verify that signing with an X509Key wrapped EdDSA key succeeds when using the underlying EdDSAEngine.
*/ */
@Test @Test
@Ignore
public void SignAndVerifyWithX509KeyAndOldEngineFails() throws InvalidKeyException, SignatureException, IOException { public void SignAndVerifyWithX509KeyAndOldEngineFails() throws InvalidKeyException, SignatureException, IOException {
X509EdDSAEngine engine = new X509EdDSAEngine(); X509EdDSAEngine engine = new X509EdDSAEngine();
KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.EDDSA_ED25519_SHA512, BigInteger.valueOf(SEED + 1)); 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]; byte[] randomBytes = new byte[TEST_DATA_SIZE];
new Random(SEED + 1).nextBytes(randomBytes); new Random(SEED + 1).nextBytes(randomBytes);
engine.initSign(keyPair.getPrivate()); engine.initSign(keyPair.getPrivate());
@ -118,17 +118,18 @@ public class X509EdDSAEngineTest {
// Now verify the signature // Now verify the signature
byte[] signature = engine.sign(); byte[] signature = engine.sign();
// engine.initVerify(publicKey); engine.initVerify(publicKey);
engine.update(randomBytes); engine.update(randomBytes);
engine.verify(signature); engine.verify(signature);
} }
/** Verify will fail if the input public key cannot be converted to EdDSA public key. */ /** Verify will fail if the input public key cannot be converted to EdDSA public key. */
@Test(expected = InvalidKeyException.class) @Test
@Ignore public void verifyWithNonSupportedKeyTypeFails() {
public void verifyWithNonSupportedKeyTypeFails() throws InvalidKeyException {
EdDSAEngine engine = new EdDSAEngine(); EdDSAEngine engine = new EdDSAEngine();
KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.ECDSA_SECP256K1_SHA256, BigInteger.valueOf(SEED)); KeyPair keyPair = Crypto.deriveKeyPairFromEntropy(Crypto.ECDSA_SECP256K1_SHA256, BigInteger.valueOf(SEED));
engine.initVerify(keyPair.getPublic()); assertThatExceptionOfType(InvalidKeyException.class).isThrownBy(() ->
engine.initVerify(keyPair.getPublic())
);
} }
} }