Example usage for org.bouncycastle.asn1 ASN1TaggedObject getEncoded

List of usage examples for org.bouncycastle.asn1 ASN1TaggedObject getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1TaggedObject getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Document

Return the default BER or DER encoding for this object.

Usage

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.j  a v a  2 s .c om
                        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.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()));
                        }//from w  ww  .j  ava 2 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);
    }
}