Example usage for org.bouncycastle.asn1 ASN1ApplicationSpecific getApplicationTag

List of usage examples for org.bouncycastle.asn1 ASN1ApplicationSpecific getApplicationTag

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1ApplicationSpecific getApplicationTag.

Prototype

public int getApplicationTag() 

Source Link

Document

Return the tag number associated with this object,

Usage

From source file:ca.trustpoint.m2m.M2mCertificateFactory.java

License:Apache License

/**
 * Generates a certificate object and initializes it with the data read from the
 * {@link java.io.InputStream InputStream} {@code inStream}.
 *
 * <p>/*from w w w. j ava 2 s .  c  om*/
 * The returned certificate object can be casted to the {@link M2mCertificate M2MCertificate}
 * class.
 *
 * <p>
 * The certificate provided in {@code inStream} must be DER-encoded and may be supplied in binary
 * or printable (Base64) encoding. If the certificate is provided in Base64 encoding, it must be
 * bounded at the beginning by -----BEGIN CERTIFICATE-----, and must be bounded at the end by
 * -----END CERTIFICATE-----.
 *
 * <p>
 * Note that if the given input stream does not support {@link java.io.InputStream#mark(int) mark}
 * and {@link java.io.InputStream#reset() reset}, this method will consume the entire input
 * stream. Otherwise, each call to this method consumes one certificate and the read position of
 * the input stream is positioned to the next available byte after the inherent end-of-certificate
 * marker. If the data in the input stream does not contain an inherent end-of-certificate marker
 * (other than EOF) and there is trailing data after the certificate is parsed, a
 * {@link java.security.cert.CertificateException CertificateException} is thrown.
 *
 * @param inStream an input stream with the certificate data.
 *
 * @return a certificate object initialized with the data from the input stream.
 *
 * @exception CertificateException on parsing errors.
 */
@Override
public Certificate engineGenerateCertificate(InputStream inStream) throws CertificateException {
    if (inStream == null) {
        throw new IllegalArgumentException("input stream is null");
    }

    try {
        ASN1InputStream aIn = new ASN1InputStream(inStream);
        ASN1ApplicationSpecific app = ASN1ApplicationSpecific.getInstance(aIn.readObject());

        aIn.close();

        int appTag = app.getApplicationTag();

        if (appTag != M2mCertificate.APPLICATION_TAG_NUMBER) {
            throw new IOException("not M2M certificate application tag: " + appTag);
        }

        ASN1Sequence seq = (ASN1Sequence) app.getObject(BERTags.SEQUENCE);
        if (seq.size() != 2) {
            throw new IOException("sequence wrong size for a M2M certificate");
        }

        // Construct M2M certificate
        M2mCertificate cert = new M2mCertificate();
        for (int i = 0; i < seq.size(); i++) {
            ASN1TaggedObject obj = (ASN1TaggedObject) seq.getObjectAt(i);
            CertificateFields tag = CertificateFields.getInstance(obj.getTagNo());

            switch (tag) {
            case TBS_CERTIFICATE:
                ASN1Sequence tbsCertificate = ASN1Sequence.getInstance(obj, false);
                parseTbsCertificate(tbsCertificate, cert);
                break;
            case CA_CALC_VALUE:
                ASN1OctetString cACalcValue = ASN1OctetString.getInstance(obj, false);
                cert.setCaCalcValue(cACalcValue.getOctets());
                break;
            default:
                throw new IOException("unknown M2M data field number: " + tag.getTagNumber());
            }
        }

        return cert;
    } catch (Exception e) {
        // Catch all exceptions and convert it to a CertificateException
        throw new CertificateException("exception on parsing certificate data", 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()));
                        }/*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);
    }
}