Example usage for org.bouncycastle.asn1 ASN1OutputStream write

List of usage examples for org.bouncycastle.asn1 ASN1OutputStream write

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 ASN1OutputStream write.

Prototype

final void write(int b) throws IOException 

Source Link

Usage

From source file:org.apache.http.contrib.auth.BouncySpnegoTokenGenerator.java

License:Apache License

public byte[] generateSpnegoDERObject(byte[] kerbTicket) throws IOException {
    DEROctetString ourKerberosTicket = new DEROctetString(kerbTicket);

    DERSequence kerbOidSeq = new DERSequence(kerbOid);
    DERTaggedObject tagged0 = new DERTaggedObject(0, kerbOidSeq);
    DERTaggedObject tagged2 = new DERTaggedObject(2, ourKerberosTicket);
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(tagged0);//  w  w  w .j  a v  a 2 s  . co  m
    v.add(tagged2);
    DERSequence seq = new DERSequence(v);
    DERTaggedObject taggedSpnego = new DERTaggedObject(0, seq);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ASN1OutputStream asn1Out = new ASN1OutputStream(out);

    ASN1Object spnegoOIDASN1 = (ASN1Object) spnegoOid.toASN1Object();
    ASN1Object taggedSpnegoASN1 = (ASN1Object) taggedSpnego.toASN1Object();

    int length = spnegoOIDASN1.getDEREncoded().length + taggedSpnegoASN1.getDEREncoded().length;
    byte[] lenBytes = writeLength(length);
    byte[] appWrap = new byte[lenBytes.length + 1];

    appWrap[0] = 0x60;
    for (int i = 1; i < appWrap.length; i++) {
        appWrap[i] = lenBytes[i - 1];
    }

    asn1Out.write(appWrap);
    asn1Out.writeObject(spnegoOid.toASN1Object());
    asn1Out.writeObject(taggedSpnego.toASN1Object());

    byte[] app = out.toByteArray();
    ASN1InputStream in = new ASN1InputStream(app);

    if (log.isDebugEnabled()) {
        int skip = 12;
        byte[] manipBytes = new byte[app.length - skip];
        for (int i = skip; i < app.length; i++) {
            manipBytes[i - skip] = app[i];
        }
        ASN1InputStream ourSpnego = new ASN1InputStream(manipBytes);
        log.debug(ASN1Dump.dumpAsString(ourSpnego.readObject()));
    }

    return in.readObject().getDEREncoded();
}

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

License:Apache License

/**
 * Write the applications directory to the token.
 * //from  ww  w.j a v a2  s . c  o m
 * @param token The token to write to.
 * @param apps The list of application templates to write.
 * @throws IOException Upon errors.
 */
protected void writeApplications(Token token, ISO7816Applications apps) throws IOException {
    token.selectMF();

    EF ef = null;

    try {
        ef = token.selectEF(DIR_PATH);
    } catch (PKCS15Exception e) {
        if (e.getErrorCode() != PKCS15Exception.ERROR_FILE_NOT_FOUND)
            throw e;
    }

    if (ef == null) {
        token.createEF(DIR_PATH, 512L,
                new EFAclImpl(TokenFileAcl.AC_ALWAYS, TokenFileAcl.AC_ALWAYS, TokenFileAcl.AC_ALWAYS,
                        TokenFileAcl.AC_ALWAYS, TokenFileAcl.AC_ALWAYS, TokenFileAcl.AC_ALWAYS,
                        TokenFileAcl.AC_ALWAYS, TokenFileAcl.AC_ALWAYS, TokenFileAcl.AC_ALWAYS));

        ef = token.selectEF(DIR_PATH);
    }

    OutputStream os = token.writeEFData();

    ASN1OutputStream aos = new ASN1OutputStream(os);

    if (apps.getApplications() != null)
        for (ISO7816ApplicationTemplate template : apps.getApplications())
            aos.writeObject(template.toASN1Object());

    aos.write(0);
    aos.write(0);
    aos.close();

}

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

License:Apache License

/**
 * Write this instance to an OuputStream. The stream is closed after
 * writing all members.//from   w ww .j ava 2s .c o m
 * 
 * @param os The stream to write to.
 * @throws IOException Upon write errors.
 */
public void writeInstance(OutputStream os) throws IOException {

    ASN1OutputStream aos = new ASN1OutputStream(os);

    // write authentication objects first, in order to be compliant
    // with opensc tokens.
    // (This eases the conception of Unit Tests against opensc
    if (this.authObjects != null)
        aos.writeObject(new DERTaggedObject(8, this.authObjects));

    if (this.privateKeys != null)
        aos.writeObject(new DERTaggedObject(0, this.privateKeys));

    if (this.publicKeys != null)
        aos.writeObject(new DERTaggedObject(1, this.publicKeys));

    if (this.trustedPublicKeys != null)
        aos.writeObject(new DERTaggedObject(2, this.trustedPublicKeys));

    // secret keys to come...

    if (this.certificates != null)
        aos.writeObject(new DERTaggedObject(4, this.certificates));

    if (this.trustedCertificates != null)
        aos.writeObject(new DERTaggedObject(5, this.trustedCertificates));

    if (this.usefulCertificates != null)
        aos.writeObject(new DERTaggedObject(6, this.usefulCertificates));

    // data objects to come...

    // write END_OF_STREAM
    aos.write(0);
    aos.write(0);
    aos.close();
}

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

License:Apache License

/**
 * Write all elements of the supplied SequenceOf to the given OutputStream. 
 * /*from  ww  w. j a  va2  s .  com*/
 * @param os The OutputStream to write to. The stream is closed by this
 *           function after writing all members of <code>seq</code>.
 * @param seq The sequence to write.
 * @throws IOException
 */
public void writeInstance(OutputStream os, SequenceOf<EntityType> seq) throws IOException {

    ASN1OutputStream aos = new ASN1OutputStream(os);

    List<EntityType> sequence = seq.getSequence();

    if (sequence != null) {

        for (EntityType e : sequence) {

            aos.writeObject(e);
        }
    }

    // write END_OF_STREAM
    aos.write(0);
    aos.write(0);
    aos.close();
}