Search in sources :

Example 21 with DerParser

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

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

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

Example 24 with DerParser

use of io.churchkey.asn1.DerParser in project ldaptive by vt-middleware.

the class DnParser method convertDnToAttributes.

/**
 * Parses the supplied DN and converts each RDN into a {@link LdapAttribute}.
 *
 * @param  dn  to parse
 *
 * @return  list of ldap attributes for each RDN
 */
public static List<LdapAttribute> convertDnToAttributes(final String dn) {
    LOGGER.trace("parsing DN: {}", dn);
    final List<LdapAttribute> attributes = new ArrayList<>();
    if (dn.isEmpty()) {
        return attributes;
    }
    int pos = 0;
    while (pos < dn.length()) {
        final int endAttrNamePos = readToChar(dn, new char[] { '=' }, pos);
        final String attrName = dn.substring(pos, endAttrNamePos);
        LOGGER.trace("read attribute name: [{}]", attrName);
        pos = endAttrNamePos;
        // error if char isn't an '='
        if (pos >= dn.length() || dn.charAt(pos++) != '=') {
            throw new IllegalArgumentException("Invalid DN: " + dn);
        }
        final int endAttrValuePos = readToChar(dn, new char[] { '+', ',' }, pos);
        String attrValue = dn.substring(pos, endAttrValuePos);
        LOGGER.trace("read attribute value: [{}]", attrValue);
        attrValue = attrValue.trim();
        // error if attribute value is empty
        if (attrValue.isEmpty()) {
            throw new IllegalArgumentException("Invalid DN: " + dn);
        }
        if (attrValue.startsWith("#")) {
            final DERParser parser = new DERParser();
            final OctetStringHandler handler = new OctetStringHandler();
            parser.registerHandler(HEX_PATH, handler);
            final String hexData = attrValue.substring(1);
            parser.parse(new DefaultDERBuffer(decodeHexValue(hexData.toCharArray())));
            attributes.add(new LdapAttribute(attrName.trim(), handler.getDecodedValue()));
        } else {
            attributes.add(new LdapAttribute(attrName.trim(), decodeStringValue(attrValue)));
        }
        pos = endAttrValuePos + 1;
    }
    LOGGER.debug("Parsed DN into: {}", attributes);
    return attributes;
}
Also used : DefaultDERBuffer(org.ldaptive.asn1.DefaultDERBuffer) ArrayList(java.util.ArrayList) DERParser(org.ldaptive.asn1.DERParser)

Example 25 with DerParser

use of io.churchkey.asn1.DerParser in project ldaptive by vt-middleware.

the class GetStatsControl method decode.

@Override
public void decode(final DERBuffer encoded) {
    final DERParser parser = new DERParser();
    parser.registerHandler(THREAD_COUNT_PATH, new IntegerHandler(this, "threadCount"));
    parser.registerHandler(CALL_TIME_PATH, new IntegerHandler(this, "callTime"));
    parser.registerHandler(ENTRIES_RETURNED_PATH, new IntegerHandler(this, "entriesReturned"));
    parser.registerHandler(ENTRIES_VISITED_PATH, new IntegerHandler(this, "entriesVisited"));
    parser.registerHandler(FILTER_PATH, new StringHandler(this, "filter"));
    parser.registerHandler(INDEX_PATH, new StringHandler(this, "index"));
    parser.registerHandler(PAGES_REFERENCED_PATH, new IntegerHandler(this, "pagesReferenced"));
    parser.registerHandler(PAGES_READ_PATH, new IntegerHandler(this, "pagesRead"));
    parser.registerHandler(PAGES_PREREAD_PATH, new IntegerHandler(this, "pagesPreread"));
    parser.registerHandler(PAGES_DIRTIED_PATH, new IntegerHandler(this, "pagesDirtied"));
    parser.registerHandler(PAGES_REDIRTIED_PATH, new IntegerHandler(this, "pagesRedirtied"));
    parser.registerHandler(LOG_RECORD_COUNT_PATH, new IntegerHandler(this, "logRecordCount"));
    parser.registerHandler(LOG_RECORD_BYTES_PATH, new IntegerHandler(this, "logRecordBytes"));
    parser.parse(encoded);
}
Also used : DERParser(org.ldaptive.asn1.DERParser)

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