Example usage for org.bouncycastle.asn1 DERApplicationSpecific getObject

List of usage examples for org.bouncycastle.asn1 DERApplicationSpecific getObject

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DERApplicationSpecific getObject.

Prototype

public ASN1Primitive getObject(int derTagNo) throws IOException 

Source Link

Document

Return the enclosed object assuming implicit tagging.

Usage

From source file:de.fraunhofer.fokus.openeid.pace.PaceECDH.java

License:Open Source License

public byte[] parseGetPointResponse(byte[] response) throws IOException {
    DERApplicationSpecific dataObject = (DERApplicationSpecific) DERApplicationSpecific.fromByteArray(response);
    ASN1Sequence sequence = ASN1Sequence.getInstance(dataObject.getObject(DERTags.SEQUENCE));
    DERTaggedObject tagged = (DERTaggedObject) sequence.getObjectAt(0);
    DEROctetString octetString = (DEROctetString) tagged.getObjectParser(DERTags.OCTET_STRING, false);
    return octetString.getOctets();
}

From source file:de.fraunhofer.fokus.openeid.structure.SequenceData.java

License:Open Source License

public SequenceData(DEREncodable dataObject) throws IOException {
    if (dataObject instanceof DERApplicationSpecific) {
        DERApplicationSpecific applicationSpecific = (DERApplicationSpecific) dataObject;
        DERObject implicitlyTaggedSequence = applicationSpecific.getObject(DERTags.SEQUENCE);
        this.dataSequence = ASN1Sequence.getInstance(implicitlyTaggedSequence);
    } else {/*w  ww .  j  a va  2  s .  co m*/
        throw new IOException(
                "Passed object is not of type DERApplicationSpecific. Only such objects can be used with this class");
    }
}

From source file:de.tsenger.animamea.asn1.AmECPublicKey.java

License:Open Source License

public static AmECPublicKey getInstance(byte[] bytes) throws IOException {
    DERApplicationSpecific seq = DERApplicationSpecific.getInstance(bytes);
    AmECPublicKey ecPubKey = new AmECPublicKey(ASN1Sequence.getInstance(seq.getObject(16)));
    return ecPubKey;
}

From source file:de.tsenger.animamea.asn1.CVCertificate.java

License:Open Source License

public CVCertificate(byte[] in) throws IllegalArgumentException, IOException {
    ASN1StreamParser asn1Parser = new ASN1StreamParser(in);

    DERApplicationSpecific cvcert = (DERApplicationSpecific) asn1Parser.readObject();
    if (cvcert.getApplicationTag() != 0x21)
        throw new IllegalArgumentException("Can't find a CV Certificate");

    ASN1Sequence derCert = (ASN1Sequence) cvcert.getObject(BERTags.SEQUENCE); // Das CV Cerificate ist eine Sequence

    DERApplicationSpecific body = (DERApplicationSpecific) derCert.getObjectAt(0); //Das erste Objekt des Certificates ist der Cert-Body
    if (body.getApplicationTag() != 0x4E)
        throw new IllegalArgumentException("Can't find a Body in the CV Certificate");

    certBody = new CVCertBody(body);

    DERApplicationSpecific signature = (DERApplicationSpecific) derCert.getObjectAt(1); //Das zweite Objekt des Certificates ist die Signatur
    if (signature.getApplicationTag() != 0x37)
        throw new IllegalArgumentException("Can't find a Signature in the CV Certificate");

    certSignature = new CVCertSignature(signature.getContents());

}

From source file:de.tsenger.animamea.asn1.DynamicAuthenticationData.java

License:Open Source License

/**
 * Constructor for decoding//from w  w w  . j  av a 2 s  . c om
 * @param data
 */
public DynamicAuthenticationData(byte[] data) {

    DERApplicationSpecific das = null;
    ASN1Sequence seq = null;

    try {
        das = (DERApplicationSpecific) DERApplicationSpecific.fromByteArray(data);
        seq = ASN1Sequence.getInstance(das.getObject(BERTags.SEQUENCE));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    for (int i = 0; i < seq.size(); i++) {
        DERTaggedObject temp = (DERTaggedObject) seq.getObjectAt(i);
        objects.add(temp);
    }

}

From source file:pro.javacard.gp.GlobalPlatform.java

License:Open Source License

private void parse_select_response(byte[] fci) throws GPException {
    try (ASN1InputStream ais = new ASN1InputStream(fci)) {
        if (ais.available() > 0) {
            // Read FCI
            DERApplicationSpecific fcidata = (DERApplicationSpecific) ais.readObject();
            // FIXME System.out.println(ASN1Dump.dumpAsString(fcidata, true));
            if (fcidata.getApplicationTag() == 15) {
                ASN1Sequence s = ASN1Sequence.getInstance(fcidata.getObject(BERTags.SEQUENCE));
                for (ASN1Encodable e : Lists.newArrayList(s.iterator())) {
                    ASN1TaggedObject t = DERTaggedObject.getInstance(e);
                    if (t.getTagNo() == 4) {
                        // ISD AID
                        ASN1OctetString isdaid = DEROctetString.getInstance(t.getObject());
                        AID detectedAID = new AID(isdaid.getOctets());
                        if (sdAID == null) {
                            logger.debug("Auto-detected ISD AID: " + detectedAID);
                        }/*from  w w  w .ja  v a2s  . com*/
                        if (sdAID != null && !detectedAID.equals(sdAID)) {
                            giveStrictWarning("SD AID in FCI does not match the requested AID!");
                        }
                        this.sdAID = sdAID == null ? detectedAID : sdAID;
                    } else if (t.getTagNo() == 5) {
                        // Proprietary, usually a sequence
                        if (t.getObject() instanceof ASN1Sequence) {
                            ASN1Sequence prop = ASN1Sequence.getInstance(t.getObject());
                            for (ASN1Encodable enc : Lists.newArrayList(prop.iterator())) {
                                ASN1Primitive proptag = enc.toASN1Primitive();
                                if (proptag instanceof DERApplicationSpecific) {
                                    DERApplicationSpecific isddata = (DERApplicationSpecific) proptag;
                                    if (isddata.getApplicationTag() == 19) {
                                        spec = GPData.get_version_from_card_data(isddata.getEncoded());
                                        logger.debug("Auto-detected GP version: " + spec);
                                    }
                                } else if (proptag instanceof DERTaggedObject) {
                                    DERTaggedObject tag = (DERTaggedObject) proptag;
                                    if (tag.getTagNo() == 101) {
                                        setBlockSize(DEROctetString.getInstance(tag.getObject()));
                                    } else if (tag.getTagNo() == 110) {
                                        logger.debug("Lifecycle data (ignored): "
                                                + HexUtils.bin2hex(tag.getObject().getEncoded()));
                                    } else {
                                        logger.info("Unknown/unhandled tag in FCI proprietary data: "
                                                + HexUtils.bin2hex(tag.getEncoded()));
                                    }
                                } else {
                                    throw new GPException("Unknown data from card: "
                                            + HexUtils.bin2hex(proptag.getEncoded()));
                                }
                            }
                        } else {
                            // Except Feitian cards which have a plain nested tag
                            if (t.getObject() instanceof DERTaggedObject) {
                                DERTaggedObject tag = (DERTaggedObject) t.getObject();
                                if (tag.getTagNo() == 101) {
                                    setBlockSize(DEROctetString.getInstance(tag.getObject()));
                                } else {
                                    logger.info("Unknown/unhandled tag in FCI proprietary data: "
                                            + HexUtils.bin2hex(tag.getEncoded()));
                                }
                            }
                        }
                    } else {
                        logger.info("Unknown/unhandled tag in FCI: " + HexUtils.bin2hex(t.getEncoded()));
                    }
                }
            } else {
                throw new GPException("Unknown data from card: " + HexUtils.bin2hex(fci));
            }
        }
    } catch (IOException | ClassCastException e) {
        throw new GPException("Invalid data: " + e.getMessage(), e);
    }

}

From source file:pro.javacard.gp.GPData.java

License:Open Source License

public static GPSpec get_version_from_card_data(byte[] data) throws GPException {
    try (ASN1InputStream ais = new ASN1InputStream(data)) {
        if (ais.available() > 0) {
            // Read card recognition data
            DERApplicationSpecific card_data = (DERApplicationSpecific) ais.readObject();
            ASN1Sequence seq = (ASN1Sequence) card_data.getObject(BERTags.SEQUENCE);
            for (ASN1Encodable p : Lists.newArrayList(seq.iterator())) {
                if (p instanceof ASN1ObjectIdentifier) {
                    ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) p;
                    // Must be fixed
                    if (!oid.toString().equalsIgnoreCase("1.2.840.114283.1")) {
                        throw new GPDataException("Invalid CardRecognitionData: " + oid.toString());
                    }//from w  w  w . ja  va2  s.c  om
                } else if (p instanceof DERApplicationSpecific) {
                    DERApplicationSpecific tag = (DERApplicationSpecific) p;
                    int n = tag.getApplicationTag();
                    if (n == 0) {
                        // Version
                        String oid = ASN1ObjectIdentifier.getInstance(tag.getObject()).toString();

                        if (oid.equalsIgnoreCase("1.2.840.114283.2.2.1.1")) {
                            return GPSpec.GP211;
                        } else if (oid.equalsIgnoreCase("1.2.840.114283.2.2.2")) {
                            return GPSpec.GP22;
                        } else if (oid.equals("1.2.840.114283.2.2.2.1")) {
                            return GPSpec.GP22; // TODO: no need to differentiate currently
                        } else {
                            throw new GPDataException("Invalid GP version OID: " + oid);
                        }
                    }
                } else {
                    throw new GPDataException("Invalid type in card data", p.toASN1Primitive().getEncoded());
                }
            }
        }
    } catch (IOException | ClassCastException e) {
        throw new GPDataException("Invalid data: " + e.getMessage());
    }
    // Default to GP211
    return GPSpec.GP211;
}

From source file:pro.javacard.gp.GPRegistry.java

License:Open Source License

private void populate_tags(byte[] data, Kind type) throws GPDataException {
    try (ASN1InputStream ais = new ASN1InputStream(data)) {
        while (ais.available() > 0) {
            DERApplicationSpecific registry_data = (DERApplicationSpecific) ais.readObject();
            // System.out.println(ASN1Dump.dumpAsString(registry_data, true));
            if (registry_data.getApplicationTag() == 3) {
                // XXX: a bit ugly and wasting code, we populate both objects but add only one
                GPRegistryEntryApp app = new GPRegistryEntryApp();
                GPRegistryEntryPkg pkg = new GPRegistryEntryPkg();
                ASN1Sequence seq = (ASN1Sequence) registry_data.getObject(BERTags.SEQUENCE);
                for (ASN1Encodable p : Lists.newArrayList(seq.iterator())) {
                    if (p instanceof DERApplicationSpecific) {
                        ASN1ApplicationSpecific entry = DERApplicationSpecific.getInstance(p);
                        if (entry.getApplicationTag() == 15) {
                            AID aid = new AID(entry.getContents());
                            app.setAID(aid);
                            pkg.setAID(aid);
                        } else if (entry.getApplicationTag() == 5) {
                            // privileges
                            Privileges privs = Privileges.fromBytes(entry.getContents());
                            app.setPrivileges(privs);
                        } else if (entry.getApplicationTag() == 4) {
                            AID a = new AID(entry.getContents());
                            app.setLoadFile(a);
                        } else if (entry.getApplicationTag() == 12) {
                            AID a = new AID(entry.getContents());
                            app.setDomain(a);
                            pkg.setDomain(a);
                        } else if (entry.getApplicationTag() == 14) {
                            pkg.setVersion(entry.getContents());
                        } else {
                            // XXX there are cards that have unknown tags.
                            // Normally we'd like to avoid having proprietary data
                            // but the rest of the response parses OK. So just ignore these
                            // tags instead of throwing an exception
                            logger.warn("Unknown tag: " + HexUtils.bin2hex(entry.getEncoded()));
                        }/*  ww w  . j  a va2 s .  c o m*/
                    } else if (p instanceof DERTaggedObject) {
                        ASN1TaggedObject tag = DERTaggedObject.getInstance(p);
                        if (tag.getTagNo() == 112) { // lifecycle
                            ASN1OctetString lc = DEROctetString.getInstance(tag, false);
                            app.setLifeCycle(lc.getOctets()[0] & 0xFF);
                            pkg.setLifeCycle(lc.getOctets()[0] & 0xFF);
                        } else if (tag.getTagNo() == 4) { // Executable module AID
                            ASN1OctetString lc = DEROctetString.getInstance(tag, false);
                            AID a = new AID(lc.getOctets());
                            pkg.addModule(a);
                        } else {
                            logger.warn("Unknown data: " + HexUtils.bin2hex(tag.getEncoded()));
                        }
                    }
                }
                // Construct entry
                if (type == Kind.ExecutableLoadFile) {
                    pkg.setType(type);
                    add(pkg);
                } else {
                    app.setType(type);
                    add(app);
                }
            } else {
                throw new GPDataException("Invalid tag", registry_data.getEncoded());
            }
        }
    } catch (IOException e) {
        throw new GPDataException("Invalid data", e);
    }
}