Search in sources :

Example 11 with Key

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

the class BeginPublicKeyTest method assertRsaDecode.

public static void assertRsaDecode(final Decoder decoder, final String algorithm, final int bits) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    final Resource resource = Resource.resource(algorithm, bits, 256);
    final KeyFactory rsa = KeyFactory.getInstance(algorithm.toUpperCase());
    final RSAPublicKey expected = (RSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
    final Key key = decoder.decode(resource.bytes("public.pkcs8.pem"));
    assertEquals(Key.Algorithm.RSA, key.getAlgorithm());
    assertEquals(Key.Type.PUBLIC, key.getType());
    assertEquals(Key.Format.PEM, key.getFormat());
    final RSAPublicKey actual = (RSAPublicKey) key.getKey();
    assertEquals(expected.getPublicExponent(), actual.getPublicExponent());
    assertEquals(expected.getModulus(), actual.getModulus());
    {
        // Export to PEM
        final String exported = new String(key.encode(Key.Format.PEM));
        assertEquals(new String(resource.bytes("public.pkcs8.pem")), exported);
    }
    {
        // Export to OPENSSH
        // PEM Public Keys do not have comments, so remove the comment from the expected output
        final String exported = new String(key.encode(Key.Format.OPENSSH));
        assertEquals(new String(resource.bytes("public.openssh")).replace(" dblevins@mingus.lan", ""), exported);
    }
    {
        // Export to JWK
        final String exported = new String(key.encode(Key.Format.JWK));
        JsonAsserts.assertJson(new String(resource.bytes("public.jwk")), exported);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) Resource(io.churchkey.Resource) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) Key(io.churchkey.Key) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey)

Example 12 with Key

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

the class BeginPublicKeyTest method assertDsaDecode.

public static void assertDsaDecode(final Decoder decoder, final String algorithm, final int bits) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    final Resource resource = Resource.resource(algorithm, bits, 256);
    final KeyFactory rsa = KeyFactory.getInstance(algorithm.toUpperCase());
    final DSAPublicKey expected = (DSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
    final Key key = decoder.decode(resource.bytes("public.pkcs8.pem"));
    assertEquals(Key.Algorithm.DSA, key.getAlgorithm());
    assertEquals(Key.Type.PUBLIC, key.getType());
    assertEquals(Key.Format.PEM, key.getFormat());
    final DSAPublicKey actual = (DSAPublicKey) key.getKey();
    assertEquals(expected.getY(), actual.getY());
    assertEquals(expected.getParams().getG(), actual.getParams().getG());
    assertEquals(expected.getParams().getP(), actual.getParams().getP());
    assertEquals(expected.getParams().getQ(), actual.getParams().getQ());
    {
        // Export to PEM
        final String exported = new String(key.encode(Key.Format.PEM));
        assertEquals(new String(resource.bytes("public.pkcs8.pem")), exported);
    }
    {
        // Export to OPENSSH
        // PEM Public Keys do not have comments, so remove the comment from the expected output
        final String exported = new String(key.encode(Key.Format.OPENSSH));
        assertEquals(new String(resource.bytes("public.openssh")).replace(" dblevins@mingus.lan", ""), exported);
    }
}
Also used : Resource(io.churchkey.Resource) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) Key(io.churchkey.Key) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 13 with Key

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

the class BeginRsaPublicKeyTest method assertDecode.

public static void assertDecode(final Decoder decoder, final Resource resource) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    final KeyFactory rsa = KeyFactory.getInstance("RSA");
    final RSAPublicKey expected = (RSAPublicKey) rsa.generatePublic(new X509EncodedKeySpec(resource.bytes("public.pkcs8.der")));
    final Key key = decoder.decode(resource.bytes("public.pkcs1.pem"));
    assertEquals(Key.Algorithm.RSA, key.getAlgorithm());
    final RSAPublicKey actual = (RSAPublicKey) key.getKey();
    assertEquals(expected.getPublicExponent(), actual.getPublicExponent());
    assertEquals(expected.getModulus(), actual.getModulus());
    {
        // Export to PEM
        final String exported = new String(key.encode(Key.Format.PEM));
        assertEquals(new String(resource.bytes("public.pkcs8.pem")), exported);
    }
    {
        // Export to OPENSSH
        // PEM Public Keys do not have comments, so remove the comment from the expected output
        final String exported = new String(key.encode(Key.Format.OPENSSH));
        assertEquals(new String(resource.bytes("public.openssh")).replace(" dblevins@mingus.lan", ""), exported);
    }
    {
        // Export to JWK
        final String exported = new String(key.encode(Key.Format.JWK));
        JsonAsserts.assertJson(new String(resource.bytes("public.jwk")), exported);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) Key(io.churchkey.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey)

Example 14 with Key

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

the class OpenSSHPrivateKeyTest method rsa.

@Test
public void rsa() throws Exception {
    final Decoder decoder = Keys::decode;
    final Resource resource = Resource.resource("opensshrsa", 2048, 256);
    final KeyFactory rsa = KeyFactory.getInstance("RSA");
    final RSAPrivateCrtKey expected = (RSAPrivateCrtKey) rsa.generatePrivate(new PKCS8EncodedKeySpec(resource.bytes("private.pkcs8.der")));
    final byte[] bytes = resource.bytes("private.openssh");
    final Key key = decoder.decode(bytes);
    assertEquals(Key.Algorithm.RSA, key.getAlgorithm());
    assertEquals(Key.Format.OPENSSH, key.getFormat());
    assertEquals(Key.Type.PRIVATE, key.getType());
    final RSAPrivateCrtKey actual = (RSAPrivateCrtKey) key.getKey();
    KeyAsserts.assertRsaPrivateKey(expected, actual);
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Resource(io.churchkey.Resource) Decoder(io.churchkey.Decoder) KeyFactory(java.security.KeyFactory) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) ECPrivateKey(java.security.interfaces.ECPrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Test(org.junit.Test)

Example 15 with Key

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

the class JwkSecretKeyTest method testHs256SecretKey.

@Test
public void testHs256SecretKey() throws Exception {
    final String jwk = "" + "{ \"kty\": \"oct\",\n" + "  \"use\": \"sig\",\n" + "  \"kid\": \"orange-1234\",\n" + "  \"k\": \"VZ-0QGLZ2P_RPUSW10CIu0WMyXq-ND2pmDYzA0OTKW" + "THlp5iac5K4VeiRr-_BOoXJ4X2fSTt4nHwo_quta7j" + "JJKT4PEWyYanBSFsi0DW7owT-HExAGDyJtHUtNw5xs" + "s8Nj6OxNPv6rROE-kevhL2wB9cqgdIscbvDhras39c" + "wfs\",\n" + "  \"alg\": \"HS256\"\n" + "}";
    final Key key = Keys.decode(jwk.getBytes());
    assertEquals("orange-1234", key.getAttribute("kid"));
    assertEquals("sig", key.getAttribute("use"));
    assertEquals("HS256", key.getAttribute("alg"));
    assertTrue(!key.getAttributes().containsKey("kty"));
    assertTrue(!key.getAttributes().containsKey("k"));
    assertEquals(3, key.getAttributes().size());
    final String encoded = "VZ-0QGLZ2P_RPUSW10CIu0WMyXq-ND2" + "pmDYzA0OTKWTHlp5iac5K4VeiRr-_BOoXJ4X2fSTt4nHwo_" + "quta7jJJKT4PEWyYanBSFsi0DW7owT-HExAGDyJtHUtNw5x" + "ss8Nj6OxNPv6rROE-kevhL2wB9cqgdIscbvDhras39cwfs";
    assertArrayEquals(key.getKey().getEncoded(), Base64.getUrlDecoder().decode(encoded));
    assertEquals(key.getAlgorithm(), Key.Algorithm.OCT);
    assertEquals(key.getFormat(), Key.Format.JWK);
    assertEquals(key.getType(), Key.Type.SECRET);
}
Also used : Key(io.churchkey.Key) Test(org.junit.Test)

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