Search in sources :

Example 51 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object 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 52 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object 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)

Example 53 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object in project churchkey by tomitribe.

the class BeginRsaPrivateKey method decode.

public static Key decode(final byte[] bytes) {
    try {
        final DerParser parser = new DerParser(bytes);
        final Asn1Object sequence = parser.readObject();
        if (sequence.getType() != Asn1Type.SEQUENCE) {
            throw new IllegalArgumentException("Invalid DER: not a sequence");
        }
        // Parse inside the sequence
        final DerParser p = sequence.createParser();
        // Skip version
        p.readObject();
        final Rsa.Private build = Rsa.Private.builder().modulus(p.readObject().asInteger()).publicExponent(p.readObject().asInteger()).privateExponent(p.readObject().asInteger()).primeP(p.readObject().asInteger()).primeQ(p.readObject().asInteger()).primeExponentP(p.readObject().asInteger()).primeExponentQ(p.readObject().asInteger()).crtCoefficient(p.readObject().asInteger()).build();
        final RSAPrivateCrtKey privateKey = build.toKey();
        final RSAPublicKey publicKey = build.toPublic().toKey();
        return new Key(privateKey, publicKey, Key.Type.PRIVATE, Key.Algorithm.RSA, Key.Format.PEM);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : Rsa(io.churchkey.rsa.Rsa) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) DerParser(io.churchkey.asn1.DerParser) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) RSAPublicKey(java.security.interfaces.RSAPublicKey) Key(io.churchkey.Key) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) Asn1Object(io.churchkey.asn1.Asn1Object)

Example 54 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object in project churchkey by tomitribe.

the class EcCurveParams method parseSequence.

public static ECParameterSpec parseSequence(final Asn1Object d1o1) throws IOException {
    final ECField field;
    final EllipticCurve ellipticCurve;
    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 Oid oid = d3o1.asOID();
        if (primeField.equals(oid)) {
            final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.INTEGER);
            field = new ECFieldFp(d3o2.toInteger());
        } else if (characteristicTwoField.equals(oid)) {
            final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.SEQUENCE);
            {
                final DerParser d4 = new DerParser(d3o2.getValue());
                final Asn1Object d4o1 = d4.readObject().assertType(Asn1Type.INTEGER);
                final Asn1Object d4o2 = d4.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
                final Oid basis = d4o2.asOID();
                if (ppBasis.equals(basis)) {
                    final Asn1Object d4o3 = d4.readObject().assertType(Asn1Type.SEQUENCE);
                    {
                        final DerParser d5 = new DerParser(d4o3.getValue());
                        final Asn1Object d5o1 = d5.readObject().assertType(Asn1Type.INTEGER);
                        final Asn1Object d5o2 = d5.readObject().assertType(Asn1Type.INTEGER);
                        final Asn1Object d5o3 = d5.readObject().assertType(Asn1Type.INTEGER);
                        field = new ECFieldF2m(d4o1.asInteger().intValue(), new int[] { d5o3.asInteger().intValue(), d5o2.asInteger().intValue(), d5o1.asInteger().intValue() });
                    }
                } else if (tpBasis.equals(basis)) {
                    final Asn1Object d5o1 = d4.readObject().assertType(Asn1Type.INTEGER);
                    field = new ECFieldF2m(d4o1.asInteger().intValue(), new int[] { d5o1.asInteger().intValue() });
                } else {
                    throw new UnsupportedOperationException("Unsupported characteristic-two-basis " + basis);
                }
            }
        } else {
            throw new UnsupportedOperationException(oid.toString());
        }
    }
    final Asn1Object d2o3 = d2.readObject().assertType(Asn1Type.SEQUENCE);
    {
        final DerParser d3 = new DerParser(d2o3.getValue());
        final Asn1Object d3o1 = d3.readObject().assertType(Asn1Type.OCTET_STRING);
        final Asn1Object d3o2 = d3.readObject().assertType(Asn1Type.OCTET_STRING);
        final Asn1Object d3o3 = d3.readObject();
        final BigInteger a = d3o1.toInteger();
        final BigInteger b = d3o2.toInteger();
        if (d3o3 == null) {
            ellipticCurve = new EllipticCurve(field, a, b);
        } else {
            ellipticCurve = new EllipticCurve(field, a, b, d3o3.getPureValueBytes());
        }
    }
    final Asn1Object d2o4 = d2.readObject().assertType(Asn1Type.OCTET_STRING);
    final Asn1Object d2o5 = d2.readObject().assertType(Asn1Type.INTEGER);
    final Asn1Object d2o6 = d2.readObject().assertType(Asn1Type.INTEGER);
    final ECPoint point = EcPoints.fromBytes(d2o4.getPureValueBytes());
    return new ECParameterSpec(ellipticCurve, point, d2o5.toInteger(), d2o6.toInteger().intValue());
}
Also used : ECField(java.security.spec.ECField) ECFieldFp(java.security.spec.ECFieldFp) EllipticCurve(java.security.spec.EllipticCurve) ECParameterSpec(java.security.spec.ECParameterSpec) BigInteger(java.math.BigInteger) DerParser(io.churchkey.asn1.DerParser) ECFieldF2m(java.security.spec.ECFieldF2m) Oid(io.churchkey.asn1.Oid) ECPoint(java.security.spec.ECPoint) Asn1Object(io.churchkey.asn1.Asn1Object)

Example 55 with ASN1Object

use of com.github.zhenwei.core.asn1.ASN1Object in project churchkey by tomitribe.

the class EcCurveParams method parseOid.

public static Oid parseOid(final byte[] data) throws IOException {
    final DerParser d1 = new DerParser(data);
    final Asn1Object d1o1 = d1.readObject().assertType(Asn1Type.OBJECT_IDENTIFIER);
    return d1o1.asOID();
}
Also used : DerParser(io.churchkey.asn1.DerParser) Asn1Object(io.churchkey.asn1.Asn1Object)

Aggregations

IOException (java.io.IOException)37 Asn1Object (com.android.hotspot2.asn1.Asn1Object)25 ASN1Object (org.bouncycastle.asn1.ASN1Object)20 ArrayList (java.util.ArrayList)16 Asn1Constructed (com.android.hotspot2.asn1.Asn1Constructed)15 HashMap (java.util.HashMap)15 Asn1Object (io.churchkey.asn1.Asn1Object)13 DerParser (io.churchkey.asn1.DerParser)12 X509Certificate (java.security.cert.X509Certificate)12 Asn1Integer (com.android.hotspot2.asn1.Asn1Integer)10 DERBitString (com.android.org.bouncycastle.asn1.DERBitString)10 DERIA5String (com.android.org.bouncycastle.asn1.DERIA5String)10 DERPrintableString (com.android.org.bouncycastle.asn1.DERPrintableString)10 ByteBuffer (java.nio.ByteBuffer)10 Key (io.churchkey.Key)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 I18Name (com.android.anqp.I18Name)5 Asn1Oid (com.android.hotspot2.asn1.Asn1Oid)5 Asn1String (com.android.hotspot2.asn1.Asn1String)5 OidMappings (com.android.hotspot2.asn1.OidMappings)5