Search in sources :

Example 16 with DerParser

use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.

the class EcCurveParams method parse.

public static ECParameterSpec parse(final byte[] data) throws IOException {
    final DerParser d1 = new DerParser(data);
    final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.SEQUENCE);
    return parseSequence(d1o1);
}
Also used : DerParser(io.churchkey.asn1.DerParser) Asn1Object(io.churchkey.asn1.Asn1Object)

Example 17 with DerParser

use of io.churchkey.asn1.DerParser 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 18 with DerParser

use of io.churchkey.asn1.DerParser 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 19 with DerParser

use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.

the class BeginPrivateKey method decodeDsaKey.

private static Key decodeDsaKey(final byte[] bytes) throws IOException {
    final Dsa.Private.Builder dsa = Dsa.Private.builder();
    final DerParser d1 = new DerParser(bytes);
    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.SEQUENCE);
            {
                final DerParser d4 = new DerParser(d3o2.getValue());
                dsa.p(d4.readBigInteger());
                dsa.q(d4.readBigInteger());
                dsa.g(d4.readBigInteger());
            }
        }
        final Asn1Object d2o3 = d2.readObject().assertType(Asn1Type.OCTET_STRING);
        {
            final DerParser d3 = new DerParser(d2o3.getValue());
            dsa.x(d3.readBigInteger());
            final Dsa.Private build = dsa.build();
            final DSAPrivateKey privateKey = build.toKey();
            final DSAPublicKey publicKey = build.toPublic().toKey();
            return new Key(privateKey, publicKey, Key.Type.PRIVATE, DSA, Key.Format.PEM);
        }
    }
}
Also used : Dsa(io.churchkey.dsa.Dsa) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DerParser(io.churchkey.asn1.DerParser) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECPublicKey(java.security.interfaces.ECPublicKey) Asn1Object(io.churchkey.asn1.Asn1Object) DSAPublicKey(java.security.interfaces.DSAPublicKey)

Example 20 with DerParser

use of io.churchkey.asn1.DerParser in project churchkey by tomitribe.

the class BeginPrivateKey method decodeRsaKey.

private static Key decodeRsaKey(final byte[] bytes) throws IOException {
    final DerParser d1 = new DerParser(bytes);
    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(Asn1Type.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 Rsa.Private build = 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();
                final RSAPrivateCrtKey privateKey = build.toKey();
                final RSAPublicKey publicKey = build.toPublic().toKey();
                return new Key(privateKey, publicKey, Key.Type.PRIVATE, RSA, Key.Format.PEM);
            }
        }
    }
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) BigInteger(java.math.BigInteger) DerParser(io.churchkey.asn1.DerParser) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) Key(io.churchkey.Key) RSAPublicKey(java.security.interfaces.RSAPublicKey) ECPrivateKey(java.security.interfaces.ECPrivateKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) ECPublicKey(java.security.interfaces.ECPublicKey) Asn1Object(io.churchkey.asn1.Asn1Object)

Aggregations

DERParser (org.ldaptive.asn1.DERParser)16 DerParser (io.churchkey.asn1.DerParser)13 Asn1Object (io.churchkey.asn1.Asn1Object)12 Key (io.churchkey.Key)8 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)5 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)4 BigInteger (java.math.BigInteger)4 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)4 DSAPublicKey (java.security.interfaces.DSAPublicKey)4 ECPrivateKey (java.security.interfaces.ECPrivateKey)4 ECPublicKey (java.security.interfaces.ECPublicKey)4 ArrayList (java.util.ArrayList)4 Oid (io.churchkey.asn1.Oid)3 ECPoint (java.security.spec.ECPoint)3 DefaultDERBuffer (org.ldaptive.asn1.DefaultDERBuffer)3 Dsa (io.churchkey.dsa.Dsa)2 Curve (io.churchkey.ec.Curve)2 Pem (io.churchkey.util.Pem)2