use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginPrivateKeyEcTest method encode.
/**
* Oakley-EC2N-3 and Oakley-EC2N-4 are ignored because
* they have no OID according to OpenSSL
*/
@Test
@Skip({ "Oakley-EC2N-3", "Oakley-EC2N-4" })
public void encode() throws Exception {
// Read the key
final byte[] bytes = resource.bytes("private.pkcs8." + openSslCurveName + "." + "oid" + ".pem");
final Key expected = EcKeys.decode(bytes);
// Write it back out to a PEM file
final byte[] encoded = expected.encode(Key.Format.PEM);
// Read it back from the PEM file
final Key actual = Keys.decode(encoded);
// Assert what we read is identical
final ECPrivateKey expectedKey = (ECPrivateKey) expected.getKey();
final ECPrivateKey actualKey = (ECPrivateKey) actual.getKey();
assertEquals(Hex.toString(expectedKey.getS().toByteArray()), Hex.toString(actualKey.getS().toByteArray()));
ECParameterSpecs.equals(expectedKey.getParams(), actualKey.getParams());
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginPrivateKeyEcTest method assertDecode.
private void assertDecode(final String format) throws IOException {
final byte[] bytes = resource.bytes("private.pkcs8." + openSslCurveName + "." + format + ".pem");
final Key key = EcKeys.decode(bytes);
assertEquals(Key.Algorithm.EC, key.getAlgorithm());
assertEquals(Key.Format.PEM, key.getFormat());
assertEquals(Key.Type.PRIVATE, key.getType());
final ECPrivateKey privateKey = (ECPrivateKey) key.getKey();
{
// assert private key integer
final byte[] expected = resource.bytes("private.pkcs8." + openSslCurveName + "." + format + ".txt");
final BigInteger i = new BigInteger(1, Hex.fromString(new String(expected)));
final BigInteger s = privateKey.getS();
assertEquals(i, s);
}
{
// assert curve parameters
final ECParameterSpec params = privateKey.getParams();
assertTrue(curve.isEqual(params));
}
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginPrivateKeyEcTest method assertPublicKey.
private void assertPublicKey(final String format) throws IOException {
final Key key = EcKeys.decode(resource.bytes("private.pkcs8." + openSslCurveName + "." + format + ".pem"));
final Key publicKey = key.getPublicKey();
assertNotNull(publicKey);
assertTrue(publicKey.getKey() instanceof ECPublicKey);
assertEquals(Key.Algorithm.EC, publicKey.getAlgorithm());
assertEquals(Key.Format.PEM, publicKey.getFormat());
assertEquals(Key.Type.PUBLIC, publicKey.getType());
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginPrivateKeyEcTest method verify.
@Skip("wap-wsg-idm-ecid-wtls7")
public void verify() throws Exception {
// Read the key
final Key key = EcKeys.decode(resource.bytes("private.pkcs8." + openSslCurveName + "." + "params" + ".pem"));
final byte[] sig = resource.bytes("private.pkcs8." + openSslCurveName + "." + "params" + ".sig");
final byte[] data = resource.bytes("data.txt");
final Signature instance = Signature.getInstance("SHA256withECDSA");
instance.initVerify((PublicKey) key.getPublicKey().getKey());
instance.update(data);
try {
assertTrue(instance.verify(Base64.getDecoder().decode(new String(sig).trim())));
} catch (SignatureException e) {
throw new AssertionError(openSslCurveName, e);
}
}
use of io.churchkey.Key in project churchkey by tomitribe.
the class BeginPrivateKeyEcTest method roundTrip.
/**
* Verify that what we are able to encode/decode the key
* and get the same result as the key we first encoded
*/
@Test
@Skip({ "Oakley-EC2N-3", "Oakley-EC2N-4" })
public void roundTrip() throws IOException {
final Key read = EcKeys.decode(resource.bytes("private.pkcs8." + openSslCurveName + ".oid.pem"));
final byte[] encode = read.encode(Key.Format.PEM);
final Key written = Keys.decode(encode);
KeyAsserts.assertEcPrivateKey((ECPrivateKey) read.getKey(), (ECPrivateKey) written.getKey());
}
Aggregations