Search in sources :

Example 1 with Key

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());
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) PublicKey(java.security.PublicKey) PrivateKey(java.security.PrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey) Test(org.junit.Test) Skip(io.churchkey.Skip)

Example 2 with Key

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));
    }
}
Also used : ECPrivateKey(java.security.interfaces.ECPrivateKey) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) PublicKey(java.security.PublicKey) PrivateKey(java.security.PrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey)

Example 3 with Key

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());
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) PublicKey(java.security.PublicKey) PrivateKey(java.security.PrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey)

Example 4 with Key

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);
    }
}
Also used : Signature(java.security.Signature) SignatureException(java.security.SignatureException) Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) PublicKey(java.security.PublicKey) PrivateKey(java.security.PrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey) Skip(io.churchkey.Skip)

Example 5 with Key

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());
}
Also used : Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) PublicKey(java.security.PublicKey) PrivateKey(java.security.PrivateKey) ECPublicKey(java.security.interfaces.ECPublicKey) Test(org.junit.Test) Skip(io.churchkey.Skip)

Aggregations

Key (io.churchkey.Key)66 RSAPublicKey (java.security.interfaces.RSAPublicKey)33 Test (org.junit.Test)31 Resource (io.churchkey.Resource)30 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)30 ECPrivateKey (java.security.interfaces.ECPrivateKey)27 ECPublicKey (java.security.interfaces.ECPublicKey)25 DSAPublicKey (java.security.interfaces.DSAPublicKey)21 KeyFactory (java.security.KeyFactory)20 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)18 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)12 PublicKey (java.security.PublicKey)11 BigInteger (java.math.BigInteger)10 PrivateKey (java.security.PrivateKey)10 Asn1Object (io.churchkey.asn1.Asn1Object)8 DerParser (io.churchkey.asn1.DerParser)8 Decoder (io.churchkey.Decoder)7 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)7 IOException (java.io.IOException)5 UncheckedIOException (java.io.UncheckedIOException)5