Search in sources :

Example 1 with Skip

use of io.churchkey.Skip 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 Skip

use of io.churchkey.Skip 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 3 with Skip

use of io.churchkey.Skip 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)

Example 4 with Skip

use of io.churchkey.Skip in project churchkey by tomitribe.

the class OpenSslEcCurvesTest method oid.

/**
 * 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 oid() throws Exception {
    final byte[] bytes = resource.bytes(openSslCurveName + "-oid.pem");
    final Oid oid = (Oid) BeginEcParameters.decode(bytes);
    final Curve actual = Curve.resolve(oid);
    assertNotNull("OID could not be resolved " + oid, actual);
    if (!curve.equals(actual) && !curve.getAliases().contains(actual) && !actual.getAliases().contains(curve)) {
        fail("Expected: " + curve + ", found: " + actual);
    }
}
Also used : Curve(io.churchkey.ec.Curve) Oid(io.churchkey.asn1.Oid) Test(org.junit.Test) Skip(io.churchkey.Skip)

Example 5 with Skip

use of io.churchkey.Skip in project churchkey by tomitribe.

the class BeginPrivateKeyEcTest method sign.

@Skip("wap-wsg-idm-ecid-wtls7")
public void sign() 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.initSign((PrivateKey) key.getKey());
    instance.update(data);
    final byte[] sign = instance.sign();
    assertEquals(new String(sig).trim(), Base64.getEncoder().encodeToString(sign));
}
Also used : Signature(java.security.Signature) 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)

Aggregations

Skip (io.churchkey.Skip)6 Key (io.churchkey.Key)4 PrivateKey (java.security.PrivateKey)4 PublicKey (java.security.PublicKey)4 ECPrivateKey (java.security.interfaces.ECPrivateKey)4 ECPublicKey (java.security.interfaces.ECPublicKey)4 Test (org.junit.Test)4 Signature (java.security.Signature)2 Oid (io.churchkey.asn1.Oid)1 Curve (io.churchkey.ec.Curve)1 SignatureException (java.security.SignatureException)1 ECParameterSpec (java.security.spec.ECParameterSpec)1