Example usage for org.bouncycastle.asn1 ASN1InputStream readObject

List of usage examples for org.bouncycastle.asn1 ASN1InputStream readObject

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1InputStream readObject.

Prototype

public ASN1Primitive readObject() throws IOException 

Source Link

Usage

From source file:org.opensc.pkcs15.application.impl.PKCS15Application.java

License:Apache License

/**
 * default constructor called during instantiation of an application already
 * existing on the card.//from w w  w.  j av a 2s  .c  o  m
 * @throws IOException Upon errors reading additional token information.
 */
PKCS15Application(Token token, ISO7816ApplicationTemplate template) throws IOException {
    if (template == null)
        throw new IllegalArgumentException("PKCS15Application instantiated with template == null.");

    if (template.getAid() == null)
        throw new IllegalArgumentException("PKCS15Application instantiated with template.aid == null.");

    if (!Arrays.equals(AIDs.PKCS15_AID, template.getAid()))
        throw new IllegalArgumentException("PKCS15Application instantiated with invalid AID in template.");

    this.template = template;
    this.token = token;

    if (this.template.getDiscretionaryData() != null) {
        ByteArrayInputStream is = new ByteArrayInputStream(this.template.getDiscretionaryData());
        ASN1InputStream ais = new ASN1InputStream(is);

        this.ddo = DDO.getInstance(ais.readObject());
        ais.close();
    }
}

From source file:org.opensc.pkcs15.asn1.ISO7816ApplicationTemplate.java

License:Apache License

/**
 * @param o An ASN.1 sequence.//from w w  w .j  ava 2  s . co  m
 */
public ISO7816ApplicationTemplate(DERApplicationSpecific o) {

    ASN1InputStream ais = new ASN1InputStream(o.getContents());

    DERObject obj;

    try {
        while ((obj = ais.readObject()) != null) {
            if (!(obj instanceof DERApplicationSpecific))
                throw new IllegalArgumentException(
                        "Item of an application template is not an application specific ASN1 object.");

            DERApplicationSpecific to = (DERApplicationSpecific) obj;

            switch (to.getApplicationTag()) {
            case AID_TAG_NO:
                this.aid = to.getContents();
                break;
            case APPLICATION_DESCRIPTION_TAG_NO:
                this.description = new String(to.getContents(), utf8Encoding);
                break;
            case PATH_TAG_NO:
                this.path = to.getContents();
                break;
            case DISCRETIONARY_DATA_TAG_NO:
                this.discretionaryData = to.getContents();
                break;

            }
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("I/O error parsing ASN1 object.", e);
    }
}

From source file:org.opensc.pkcs15.asn1.PKCS15Objects.java

License:Apache License

/**
 * Parse a PKCS15Objects instance from an input stream.
 * The stream is closed after reading all members.
 * /* w  ww . j a  va 2  s.com*/
 * @param is The InputStream to read from.
 * @param context The context used for proxy instantiation.
 * @return A PKCS15Objects instance.
 * @throws IOException upon read errors.
 */
public static PKCS15Objects readInstance(InputStream is, Context context) throws IOException {
    ContextHolder.setContext(context);

    try {
        ASN1InputStream ais = new ASN1InputStream(is);

        PKCS15Objects ret = new PKCS15Objects();

        DERObject obj;

        while ((obj = ais.readObject()) != null) {
            // The internal END_OF_STREAM object of
            // ASN1InputStream does not derive from ASN1Object, while
            // all other meaningful DERObjects do, so leave the loop
            // if this is not an ASN1Object
            if (!(obj instanceof ASN1Object))
                break;

            if (!(obj instanceof ASN1TaggedObject))
                throw new IllegalArgumentException(
                        "PKCS15Objects must consist of a sequence of ASN.1 TAGGED OBJECTS.");

            ASN1TaggedObject to = (ASN1TaggedObject) obj;

            switch (to.getTagNo()) {
            case 0:
                ret.setPrivateKeys(privateKeysFactory.getInstance(to.getObject()));
                break;
            case 1:
                ret.setPublicKeys(publicKeysFactory.getInstance(to.getObject()));
                break;
            case 2:
                ret.setTrustedPublicKeys(publicKeysFactory.getInstance(to.getObject()));
                break;
            case 3:
                throw new IllegalArgumentException("SecretKeys are not supported yet.");
            case 4:
                ret.setCertificates(certificatesFactory.getInstance(to.getObject()));
                break;
            case 5:
                ret.setTrustedCertificates(certificatesFactory.getInstance(to.getObject()));
                break;
            case 6:
                ret.setUsefulCertificates(certificatesFactory.getInstance(to.getObject()));
                break;
            case 7:
                throw new IllegalArgumentException("DataObjects are not supported yet.");
            case 8:
                ret.setAuthObjects(authObjectsFactory.getInstance(to.getObject()));
                break;

            default:
                throw new IllegalArgumentException(
                        "Invalid memebr tag [" + to.getTagNo() + "] in PKCS15Objects sequence.");
            }
        }

        ais.close();
        return ret;

    } finally {
        ContextHolder.removeContext();
    }

}

From source file:org.opensc.pkcs15.asn1.proxy.StreamResolverDirectory.java

License:Apache License

@Override
public EntityType resolveReference(ReferenceType ref) {

    try {/*from   w  ww  . j a v  a2s  . com*/

        ASN1InputStream ais = new ASN1InputStream(this.streamResolver.readReference(ref));

        return this.instanceFactory.getInstance(ais.readObject());

    } catch (IOException e) {
        throw new IllegalArgumentException("Reference [" + ref + "] cannot be read.", e);
    }
}

From source file:org.opensc.pkcs15.asn1.sequence.SequenceOfFactory.java

License:Apache License

/**
 * Read an sequence from an InputStream and close the stream after reading.
 * //from w w  w  . j a v  a 2 s  .  co m
 * @param is The InputStream to read from.
 * @return The SequenceOf instance, which is the concatenation of all objects in
 *         the given InputStream.
 * @throws IOException
 */
public SequenceOf<EntityType> readInstance(InputStream is) throws IOException {

    ASN1InputStream ais = new ASN1InputStream(is);

    DERObject obj;
    SequenceOf<EntityType> ret = new SequenceOfImpl<EntityType>();

    while ((obj = ais.readObject()) != null) {
        // The internal END_OF_STREAM object of
        // ASN1InputStream does not derive from ASN1Object, while
        // all other meaningful DERObjects do, so leave the loop
        // if this is not an ASN1Object
        if (!(obj instanceof ASN1Object))
            break;

        ret.addEntity(this.instanceFactory.getInstance(obj));
    }
    is.close();
    return ret;
}

From source file:org.signserver.module.mrtdsodsigner.jmrtd.SODFile.java

License:Open Source License

/**
 * Constructs a Security Object data structure.
 *
 * @param in some inputstream/*  ww  w.j a  va2s  . c  om*/
 * @throws IOException if something goes wrong
 */
public SODFile(InputStream in) throws IOException {
    BERTLVInputStream tlvIn = new BERTLVInputStream(in);
    tlvIn.readTag();
    tlvIn.readLength();
    ASN1InputStream asn1in = new ASN1InputStream(in);
    ASN1Sequence seq = (ASN1Sequence) asn1in.readObject();
    /* DERObjectIdentifier objectIdentifier = (DERObjectIdentifier) seq.getObjectAt(0); */ /* FIXME: do we need this? */
    //DERTaggedObject o = (DERTaggedObject)seq.getObjectAt(1);
    /* TODO: where is this tagNo specified? */
    // int tagNo =  o.getTagNo();
    ASN1Sequence s2 = (ASN1Sequence) ((DERTaggedObject) seq.getObjectAt(1)).getObject();

    this.signedData = SignedData.getInstance(s2);
}

From source file:org.signserver.module.mrtdsodsigner.jmrtd.SODFile.java

License:Open Source License

/**
 * Reads the security object (containing the hashes
 * of the data groups) found in the SOd on the passport.
 * /* w w w. java 2  s. co m*/
 * @return the security object
 * 
 * @throws IOException
 */
private static LDSSecurityObject getSecurityObject(SignedData signedData) {
    try {
        ContentInfo contentInfo = signedData.getEncapContentInfo();
        byte[] content = ((DEROctetString) contentInfo.getContent()).getOctets();
        ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(content));

        LDSSecurityObject sod = new LDSSecurityObject((ASN1Sequence) in.readObject());
        Object nextObject = in.readObject();

        if (nextObject != null) {
            LOGGER.warning("extra object found after LDSSecurityObject...");
        }
        return sod;
    } catch (IOException ioe) {
        throw new IllegalStateException("Could not read security object in signedData");
    }
}

From source file:org.signserver.module.mrtdsodsigner.MRTDSODSignerUnitTest.java

License:Open Source License

public void test04LdsConfigVersion17_ok() throws Exception {
    // DG1, DG2 and default values
    Map<Integer, byte[]> dataGroups1 = new LinkedHashMap<Integer, byte[]>();
    dataGroups1.put(1, digestHelper("Dummy Value 1".getBytes(), "SHA256"));
    dataGroups1.put(2, digestHelper("Dummy Value 2".getBytes(), "SHA256"));
    final SODFile sod = signHelper(WORKER1, 12, dataGroups1, false, "SHA256", "SHA256withRSA");

    // ASN.1 Dump SODFile
    ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(sod.getEncoded()));
    ASN1Object object = in.readObject();
    LOG.info("Object: " + ASN1Dump.dumpAsString(object, true));

    //        // ANS.1 Dump LDSSecurityObject
    //        in = new ASN1InputStream(new ByteArrayInputStream(sod.getSecurityObject()));
    //        object = in.readObject();
    //        LOG.info("LDSSecurityObject: " + ASN1Dump.dumpAsString(object, true));

    assertNull("LDS version", sod.getLdsVersion());
    assertNull("Unicode version", sod.getUnicodeVersion());
}

From source file:org.signserver.module.mrtdsodsigner.MRTDSODSignerUnitTest.java

License:Open Source License

public void test05LdsConfigVersion18_ok() throws Exception {
    // DG1, DG2 and default values
    Map<Integer, byte[]> dataGroups1 = new LinkedHashMap<Integer, byte[]>();
    dataGroups1.put(1, digestHelper("Dummy Value 1".getBytes(), "SHA256"));
    dataGroups1.put(2, digestHelper("Dummy Value 2".getBytes(), "SHA256"));
    final SODFile sod = signHelper(WORKER5, 12, dataGroups1, false, "SHA256", "SHA256withRSA");

    // ASN.1 Dump
    ASN1InputStream in = new ASN1InputStream(new ByteArrayInputStream(sod.getEncoded()));
    ASN1Object object = in.readObject();
    LOG.info("Object: " + ASN1Dump.dumpAsString(object, true));

    //        // ANS.1 Dump LDSSecurityObject
    //        in = new ASN1InputStream(new ByteArrayInputStream(sod.getSecurityObject()));
    //        object = in.readObject();
    //        LOG.info("LDSSecurityObject: " + ASN1Dump.dumpAsString(object, true));

    assertEquals("LDS version", "0108", sod.getLdsVersion());
    assertEquals("Unicode version", "040000", sod.getUnicodeVersion());
}