Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions common/src/test/java/org/conscrypt/EdDsaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
Expand Down Expand Up @@ -284,4 +288,125 @@ public void testVectors() throws Exception {
assertTrue(verifier.verify(sig));
}
}

@Test
public void serializeAndDeserialize_works() throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("Ed25519", conscryptProvider);
KeyPair keyPair = keyGen.generateKeyPair();

ByteArrayOutputStream baos = new ByteArrayOutputStream(16384);
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(keyPair.getPrivate());
oos.writeObject(keyPair.getPublic());
}

ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
PrivateKey inflatedPrivateKey = (PrivateKey) ois.readObject();
PublicKey inflatedPublicKey = (PublicKey) ois.readObject();

assertEquals(inflatedPrivateKey, keyPair.getPrivate());
assertEquals(inflatedPublicKey, keyPair.getPublic());
}

@Test
public void serializePrivateKey_isEqualToTestVector() throws Exception {
byte[] pkcs8EncodedPrivateKey = decodeHex(
// PKCS#8 header
"302e020100300506032b657004220420"
// raw private key
+ "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60");
KeyFactory keyFactory = KeyFactory.getInstance("Ed25519", conscryptProvider);
PrivateKey privateKey =
keyFactory.generatePrivate(new PKCS8EncodedKeySpec(pkcs8EncodedPrivateKey));

ByteArrayOutputStream baos = new ByteArrayOutputStream(16384);
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(privateKey);
}

String expectedHexEncoding = "aced000573720024"
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
+ "4f70656e53736c4564447361507269766174654b6579" // hex("OpenSslEdDsaPrivateKey")
+ "d479f95a133abadc" // serialVersionUID
+ "0200015b000f"
+ "707269766174654b65794279746573" // hex("privateKeyBytes")
+ "7400025b427870757200025b42acf317f8060854e0020000787000000020"
+ "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60"; // private key
assertEquals(expectedHexEncoding, TestUtils.encodeHex(baos.toByteArray()));
}

@Test
public void serializePublicKey_isEqualToTestVector() throws Exception {
byte[] x509EncodedPublicKey = decodeHex(
// X.509 header
"302a300506032b6570032100"
// raw public key
+ "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a");

KeyFactory keyFactory = KeyFactory.getInstance("Ed25519", conscryptProvider);
PublicKey publicKey =
keyFactory.generatePublic(new X509EncodedKeySpec(x509EncodedPublicKey));

ByteArrayOutputStream baos = new ByteArrayOutputStream(16384);
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(publicKey);
}

String expectedHexEncoding = "aced000573720023"
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
+ "4f70656e53736c45644473615075626c69634b6579" // hex("OpenSslEdDsaPublicKey")
+ "064c7113d078e42d" // serialVersionUID
+ "0200015b000e"
+ "7075626c69634b65794279746573" // hex("publicKeyBytes")
+ "7400025b427870757200025b42acf317f8060854e0020000787000000020"
+ "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a"; // public key
assertEquals(expectedHexEncoding, TestUtils.encodeHex(baos.toByteArray()));
}

@Test
public void deserializeInvalidPrivateKey_fails() throws Exception {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's worth to add tests where we just check that the Java library does the right thing; here this is clearly an invalid stream, so it would only worth to test this if we somehow completely parse it ourselves. I think in such a case I would rather add this test before I refactor the implementation. (Anyhow, of course let's leave the test for now and you can always disagree and add more tests).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan is to add a check to the private key implementation that will change the behaviour in this test: instead of failing in "initSign", already "readObject" will fail. Adding this test now will make this much clearer in the next change, I think. So I'd prefer to keep it.

String invalidPrivateKeySerialized = "aced000573720024"
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
+ "4f70656e53736c4564447361507269766174654b6579" // hex("OpenSslEdDsaPrivateKey")
+ "d479f95a133abadc" // serialVersionUID
+ "0200015b000f"
+ "707269766174654b65794279746573" // hex("privateKeyBytes")
+ "7400025b427870757200025b42acf317f8060854e00200007870000000"
+ "1f" // size of private key (is 31, which is invalid)
+ "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f"; // private key

ByteArrayInputStream bais =
new ByteArrayInputStream(TestUtils.decodeHex(invalidPrivateKeySerialized));
ObjectInputStream ois = new ObjectInputStream(bais);

// This creates an invalid private key that can't be used to sign.
// It would be better to throw an exception when reading the object.
PrivateKey privateKey = (PrivateKey) ois.readObject();
Signature signer = Signature.getInstance("Ed25519", conscryptProvider);
assertThrows(InvalidKeyException.class, () -> signer.initSign(privateKey));
}

@Test
public void deserializeInvalidPublicKey_fails() throws Exception {
String invalidPublicKeySerialized = "aced000573720023"
+ "6f72672e636f6e7363727970742e" // hex("org.conscrypt.")
+ "4f70656e53736c45644473615075626c69634b6579" // hex("OpenSslEdDsaPublicKey")
+ "064c7113d078e42d" // serialVersionUID
+ "0200015b000e"
+ "7075626c69634b65794279746573" // hex("publicKeyBytes")
+ "7400025b427870757200025b42acf317f8060854e00200007870000000"
+ "1f" // size of public key (is 31, which is invalid)
+ "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f70751"; // public key

ByteArrayInputStream bais =
new ByteArrayInputStream(TestUtils.decodeHex(invalidPublicKeySerialized));
ObjectInputStream ois = new ObjectInputStream(bais);

// This creates an invalid public key that can't be used to verify.
// It would be better to throw an exception when reading the object.
PublicKey publicKey = (PublicKey) ois.readObject();
Signature verifier = Signature.getInstance("Ed25519", conscryptProvider);
assertThrows(InvalidKeyException.class, () -> verifier.initVerify(publicKey));
}
}
Loading