Search in sources :

Example 1 with Pem

use of io.churchkey.util.Pem in project churchkey by tomitribe.

the class OpenSSHPrivateKey method decode.

public static Key decode(final byte[] bytes) {
    final Pem pem = Pem.parse(bytes);
    try {
        final KeyInput keyInput = new KeyInput(pem.getData());
        assertString("Auth Magic", "openssh-key-v1", keyInput.readAuthMagic());
        assertString("ciphername", "none", keyInput.readString());
        assertString("kdfname", "none", keyInput.readString());
        assertString("kdf", "", keyInput.readString());
        assertInt("number of keys", 1, keyInput.readInt());
        // Ignore the public key, it is repeated in the private key
        final byte[] sshpublic = keyInput.readBytes();
        keyInput.readInt();
        // a random 32-bit int, repeated
        keyInput.readInt();
        keyInput.readInt();
        final String keyType = keyInput.readString();
        if ("ssh-rsa".equals(keyType)) {
            return readRsaPrivateKey(keyInput);
        }
        if ("ssh-dss".equals(keyType)) {
            return readPrivateDssKey(keyInput);
        }
        if ("ecdsa-sha2-nistp256".equals(keyType)) {
            return readEcdsaPrivateKey(Curve.nistp256, keyInput);
        }
        if ("ecdsa-sha2-nistp384".equals(keyType)) {
            return readEcdsaPrivateKey(Curve.nistp384, keyInput);
        }
        if ("ecdsa-sha2-nistp521".equals(keyType)) {
            return readEcdsaPrivateKey(Curve.nistp521, keyInput);
        }
        throw new UnsupportedOperationException("Unsupported key type: " + keyType);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    } catch (IOException | InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
}
Also used : Pem(io.churchkey.util.Pem) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 2 with Pem

use of io.churchkey.util.Pem in project churchkey by tomitribe.

the class BeginPublicKey method encode.

public static byte[] encode(final Key key) {
    final Pem.Builder pem = Pem.builder().type("PUBLIC KEY").wrap(64);
    final java.security.Key value = key.getKey();
    try {
        if (value instanceof RSAPublicKey) {
            final KeyFactory factory = KeyFactory.getInstance("RSA");
            final X509EncodedKeySpec keySpec = factory.getKeySpec(value, X509EncodedKeySpec.class);
            return pem.data(keySpec.getEncoded()).format().getBytes();
        }
        if (value instanceof DSAPublicKey) {
            final KeyFactory factory = KeyFactory.getInstance("DSA");
            final X509EncodedKeySpec keySpec = factory.getKeySpec(value, X509EncodedKeySpec.class);
            return pem.data(keySpec.getEncoded()).format().getBytes();
        }
        if (value instanceof ECPublicKey) {
            final KeyFactory factory = KeyFactory.getInstance("EC");
            final X509EncodedKeySpec keySpec = factory.getKeySpec(value, X509EncodedKeySpec.class);
            return pem.data(keySpec.getEncoded()).format().getBytes();
        }
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("Unsupported key algorithm", e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalStateException("Invalid Key Spec", e);
    }
    throw new UnsupportedOperationException("Unsupported key algorithm " + key.getAlgorithm());
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPublicKey(java.security.interfaces.ECPublicKey) Pem(io.churchkey.util.Pem) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 3 with Pem

use of io.churchkey.util.Pem in project churchkey by tomitribe.

the class FooTest method test3.

@Ignore
@Test
public void test3() throws Exception {
    final Resource resource = Resource.resource(BeginPrivateKeyTest.class.getSimpleName());
    final byte[] bytes = resource.bytes("openssl-rsaprivatekey-3072.pem");
    final Pem pem = Pem.parse(bytes);
    {
        final DerParser d1 = new DerParser(pem.getData());
        final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
        {
            final DerParser d2 = new DerParser(d1o1.getValue());
            final Asn1Object d2o1 = d2.readObject().assertType(Asn1Type.INTEGER);
            final Asn1Object d2o2 = d2.readObject().assertType(Asn1Type.SEQUENCE);
            {
                final DerParser d3 = new DerParser(d2o2.getValue());
                final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
                final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.NULL);
            }
            final Asn1Object d2o3 = d2.readObject().assertType(OCTET_STRING);
            {
                final DerParser d3 = new DerParser(d2o3.getValue());
                final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.SEQUENCE);
                {
                    final DerParser d4 = new DerParser(d3o1.getValue());
                    final BigInteger version = d4.readBigInteger();
                    final RSAPrivateCrtKey privateKey = Rsa.Private.builder().modulus(d4.readBigInteger()).publicExponent(d4.readBigInteger()).privateExponent(d4.readBigInteger()).primeP(d4.readBigInteger()).primeQ(d4.readBigInteger()).primeExponentP(d4.readBigInteger()).primeExponentQ(d4.readBigInteger()).crtCoefficient(d4.readBigInteger()).build().toKey();
                    final Key key1 = new Key(privateKey, Key.Type.PRIVATE, RSA, Key.Format.PEM);
                    System.out.println(key1);
                }
            }
        }
    }
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Resource(io.churchkey.Resource) Pem(io.churchkey.util.Pem) BigInteger(java.math.BigInteger) DerParser(io.churchkey.asn1.DerParser) Key(io.churchkey.Key) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Asn1Object(io.churchkey.asn1.Asn1Object) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with Pem

use of io.churchkey.util.Pem in project churchkey by tomitribe.

the class BeginEcParameters method decode.

public static Object decode(final byte[] bytes) throws IOException {
    if (!Utils.startsWith("-----BEGIN EC PARAMETERS-----", bytes)) {
        throw new IllegalArgumentException("Contents do not start with -----BEGIN EC PARAMETERS-----");
    }
    final Pem pem = Pem.parse(bytes);
    final byte[] data = pem.getData();
    final Asn1Type type = new DerParser(data).readObject().getType();
    if (type == Asn1Type.SEQUENCE) {
        return EcCurveParams.parse(data);
    }
    if (type == Asn1Type.OBJECT_IDENTIFIER) {
        return EcCurveParams.parseOid(data);
    }
    throw new UnsupportedOperationException("Unexpected ASN1 type: " + type);
}
Also used : Asn1Type(io.churchkey.asn1.Asn1Type) Pem(io.churchkey.util.Pem) DerParser(io.churchkey.asn1.DerParser)

Example 5 with Pem

use of io.churchkey.util.Pem in project churchkey by tomitribe.

the class Asn1Dump method dump.

public static String dump(final byte[] bytes) throws IOException {
    final File der = File.createTempFile("der", ".dump");
    if (bytes[0] == '-' && bytes[1] == '-') {
        final Pem pem = Pem.parse(bytes);
        IO.copy(pem.getData(), der);
    } else {
        IO.copy(bytes, der);
    }
    final PrintString err = new PrintString();
    final PrintString out = new PrintString();
    final ProcessBuilder builder = new ProcessBuilder("openssl", "asn1parse", "-i", "-inform", "DER", "-in", der.getAbsolutePath(), "-dump");
    final Process process = builder.start();
    final Future<Pipe> o = Pipe.pipe(process.getInputStream(), out);
    final Future<Pipe> e = Pipe.pipe(process.getErrorStream(), err);
    try {
        final int i = process.waitFor();
        o.get();
        e.get();
        if (i != 0) {
            throw new IllegalStateException("Exit code " + i + "\n" + err);
        }
    } catch (InterruptedException | ExecutionException exception) {
        throw new IllegalStateException(exception);
    } finally {
        out.close();
        err.close();
        der.delete();
    }
    return out.toString();
}
Also used : PrintString(org.tomitribe.util.PrintString) Pem(io.churchkey.util.Pem) Pipe(org.tomitribe.util.Pipe) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Aggregations

Pem (io.churchkey.util.Pem)5 DerParser (io.churchkey.asn1.DerParser)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 Key (io.churchkey.Key)1 Resource (io.churchkey.Resource)1 Asn1Object (io.churchkey.asn1.Asn1Object)1 Asn1Type (io.churchkey.asn1.Asn1Type)1 File (java.io.File)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 BigInteger (java.math.BigInteger)1 KeyFactory (java.security.KeyFactory)1 DSAPublicKey (java.security.interfaces.DSAPublicKey)1 ECPublicKey (java.security.interfaces.ECPublicKey)1 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)1 RSAPublicKey (java.security.interfaces.RSAPublicKey)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 ExecutionException (java.util.concurrent.ExecutionException)1 Ignore (org.junit.Ignore)1