List of usage examples for org.bouncycastle.asn1 ASN1ApplicationSpecific getContents
public byte[] getContents()
From source file:pro.javacard.gp.GPData.java
License:Open Source License
public static List<GPKeySet.GPKey> get_key_template_list(byte[] data) throws GPException { List<GPKey> r = new ArrayList<>(); try (ASN1InputStream ais = new ASN1InputStream(data)) { while (ais.available() > 0) { ASN1ApplicationSpecific keys = (DERApplicationSpecific) ais.readObject(); // System.out.println(ASN1Dump.dumpAsString(keys, true)); ASN1Sequence seq = (ASN1Sequence) keys.getObject(BERTags.SEQUENCE); for (ASN1Encodable p : Lists.newArrayList(seq.iterator())) { ASN1ApplicationSpecific key = (DERApplicationSpecific) p.toASN1Primitive(); byte[] tmpl = key.getContents(); if (tmpl.length < 4) { throw new GPDataException("Key info template shorter than 4 bytes", tmpl); }//from ww w . j av a 2 s . c o m int id = tmpl[0] & 0xFF; int version = tmpl[1] & 0xFF; int type = tmpl[2] & 0xFF; int length = tmpl[3] & 0xFF; if (type == 0xFF) { throw new GPDataException("Extended key template not yet supported", tmpl); } r.add(new GPKey(version, id, length, type)); } } } catch (IOException | ClassCastException e) { throw new GPDataException("Could not parse key template: " + e.getMessage(), e); } return r; }
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 w w . j a v a2s .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); } }