Example usage for org.bouncycastle.asn1 ASN1OutputStream ASN1OutputStream

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

Introduction

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

Prototype

public ASN1OutputStream(OutputStream os) 

Source Link

Usage

From source file:org.jruby.ext.openssl.x509store.BouncyCastleASN1FormatHandler.java

License:LGPL

@Override
public void writeDHParameters(Writer _out, DHParameterSpec params) throws IOException {
    BufferedWriter out = makeBuffered(_out);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);

    ASN1EncodableVector v = new ASN1EncodableVector();

    BigInteger value;//from   w w  w. j  a  v  a 2s . co m
    if ((value = params.getP()) != null) {
        v.add(new DERInteger(value));
    }
    if ((value = params.getG()) != null) {
        v.add(new DERInteger(value));
    }

    aOut.writeObject(new DERSequence(v));
    byte[] encoding = bOut.toByteArray();

    out.write(BEF_G + PEM_STRING_DHPARAMS + AFT);
    out.newLine();
    writeEncoded(out, encoding);
    out.write(BEF_E + PEM_STRING_DHPARAMS + AFT);
    out.newLine();
    out.flush();
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

public static void writeDSAPrivateKey(Writer _out, DSAPrivateKey obj, CipherSpec cipher, char[] passwd)
        throws IOException {
    BufferedWriter out = makeBuffered(_out);
    PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence) new ASN1InputStream(getEncoded(obj)).readObject());
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);

    DSAParameter p = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters());
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(0));
    v.add(new ASN1Integer(p.getP()));
    v.add(new ASN1Integer(p.getQ()));
    v.add(new ASN1Integer(p.getG()));

    BigInteger x = obj.getX();/*  ww w  .j a  v a 2  s .  com*/
    BigInteger y = p.getG().modPow(x, p.getP());

    v.add(new ASN1Integer(y));
    v.add(new ASN1Integer(x));

    aOut.writeObject(new DLSequence(v));
    byte[] encoding = bOut.toByteArray();

    if (cipher != null && passwd != null) {
        writePemEncrypted(out, PEM_STRING_DSA, encoding, cipher, passwd);
    } else {
        writePemPlain(out, PEM_STRING_DSA, encoding);
    }
}

From source file:org.jruby.ext.openssl.x509store.PEMInputOutput.java

License:LGPL

public static void writeDHParameters(Writer _out, DHParameterSpec params) throws IOException {
    BufferedWriter out = makeBuffered(_out);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    ASN1OutputStream aOut = new ASN1OutputStream(bOut);

    ASN1EncodableVector v = new ASN1EncodableVector();

    BigInteger value;/*from   w  ww .j a va2s .  c o  m*/
    if ((value = params.getP()) != null) {
        v.add(new ASN1Integer(value));
    }
    if ((value = params.getG()) != null) {
        v.add(new ASN1Integer(value));
    }

    aOut.writeObject(new DLSequence(v));
    byte[] encoding = bOut.toByteArray();

    out.write(BEF_G + PEM_STRING_DHPARAMS + AFT);
    out.newLine();
    writeEncoded(out, encoding);
    out.write(BEF_E + PEM_STRING_DHPARAMS + AFT);
    out.newLine();
    out.flush();
}

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

License:Apache License

/**
 * Write the applications directory to the token.
 * /*from  w w w.j av a  2 s. com*/
 * @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.attr.RSAPrivateKeyObjectImpl.java

License:Apache License

@Override
public byte[] getEncoded() {

    try {/*from   w  w  w .j  av a2  s . c o m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ASN1OutputStream aos = new ASN1OutputStream(bos);
        aos.writeObject(this.getDERObject());
        return bos.toByteArray();
    } catch (IOException e) {
        throw new SecurityException("Cannot encode PKCS#15 RSAPrivateKeyObjectImpl.", e);
    }
}

From source file:org.opensc.pkcs15.asn1.attr.RSAPublicKeyChoice.java

License:Apache License

public byte[] getEncoded() {
    try {/*from  w ww.j av a  2 s.  c  o  m*/
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ASN1OutputStream aos = new ASN1OutputStream(bos);
        aos.writeObject(this.getDERObject());
        return bos.toByteArray();
    } catch (IOException e) {
        throw new SecurityException("Cannot encode PKCS#15 RSAPublicKeyChoice.", e);
    }
}

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

License:Apache License

@Override
public DERObject toASN1Object() {

    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    ASN1OutputStream aos = new ASN1OutputStream(bos);
    try {/*from   w  ww  .j  av  a 2 s  .  co m*/

        if (this.aid != null)
            aos.writeObject(new DERApplicationSpecific(AID_TAG_NO, this.aid));

        if (this.description != null)
            aos.writeObject(new DERApplicationSpecific(APPLICATION_DESCRIPTION_TAG_NO,
                    this.description.getBytes("utf-8")));

        if (this.path != null)
            aos.writeObject(new DERApplicationSpecific(PATH_TAG_NO, this.path));

        if (this.discretionaryData != null)
            aos.writeObject(new DERApplicationSpecific(DISCRETIONARY_DATA_TAG_NO, this.discretionaryData));

        return new DERApplicationSpecific(1 | DERTags.CONSTRUCTED, bos.toByteArray());

    } catch (IOException e) {
        throw new RuntimeException("IO error contructions ASN1 representation.", e);
    }
}

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  a v  a 2  s  .c om
 * 
 * @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.proxy.StreamResolverDirectory.java

License:Apache License

@Override
public void updateEntity(ReferenceType ref, EntityType entity) {

    try {//from   w w  w . j  av  a  2  s.c o m
        ASN1OutputStream aos = new ASN1OutputStream(this.streamResolver.writeReference(ref));

        aos.writeObject(entity);
        aos.close();

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

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   w  w w  .j a v  a 2 s .c  o m
 * @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();
}