Example usage for org.bouncycastle.asn1 ASN1OutputStream writeObject

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

Introduction

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

Prototype

public void writeObject(ASN1Primitive primitive) throws IOException 

Source Link

Usage

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 . ja v a 2s.  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   ww  w .  ja  va 2  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.attr.RSAPrivateKeyObjectImpl.java

License:Apache License

@Override
public byte[] getEncoded() {

    try {//  ww w  .j  a v  a  2s.  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 {// w ww  . j av  a2  s .co  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 {//w  ww.j  a v  a2s  . c o  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  www .  j  a va  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.proxy.StreamResolverDirectory.java

License:Apache License

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

    try {/*from ww  w  .  ja  va2 s.com*/
        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 ww w.  ja v  a2 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();
}

From source file:org.silvertunnel.netlib.layer.tor.util.Encryption.java

License:Open Source License

/**
 * converts a RSAPublicKey into PKCS1-encoding (ASN.1)
 * //from ww  w  . j  a v  a2s  . c  o m
 * @param rsaPublicKey
 * @see JCERSAPublicKey
 * @return PKCS1-encoded RSA PUBLIC KEY
 */
public static byte[] getPKCS1EncodingFromRSAPublicKey(RSAPublicKey pubKeyStruct) {
    try {
        RSAPublicKeyStructure myKey = new RSAPublicKeyStructure(pubKeyStruct.getModulus(),
                pubKeyStruct.getPublicExponent());
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        ASN1OutputStream aOut = new ASN1OutputStream(bOut);
        aOut.writeObject(myKey.toASN1Object());
        return bOut.toByteArray();
    } catch (Exception e) {
        return null;
    }
}

From source file:org.sufficientlysecure.keychain.securitytoken.SecurityTokenConnection.java

License:Open Source License

private byte[] encodeSignature(byte[] signature, KeyFormat keyFormat) throws IOException {
    // Make sure the signature we received is actually the expected number of bytes long!
    switch (keyFormat.keyFormatType()) {
    case RSAKeyFormatType:
        // no encoding necessary
        int modulusLength = ((RSAKeyFormat) keyFormat).getModulusLength();
        if (signature.length != (modulusLength / 8)) {
            throw new IOException("Bad signature length! Expected " + (modulusLength / 8) + " bytes, got "
                    + signature.length);
        }//from   w  ww.java 2 s.co m
        break;

    case ECKeyFormatType:
        // "plain" encoding, see https://github.com/open-keychain/open-keychain/issues/2108
        if (signature.length % 2 != 0) {
            throw new IOException("Bad signature length!");
        }
        final byte[] br = new byte[signature.length / 2];
        final byte[] bs = new byte[signature.length / 2];
        for (int i = 0; i < br.length; ++i) {
            br[i] = signature[i];
            bs[i] = signature[br.length + i];
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ASN1OutputStream out = new ASN1OutputStream(baos);
        out.writeObject(new DERSequence(new ASN1Encodable[] { new ASN1Integer(br), new ASN1Integer(bs) }));
        out.flush();
        signature = baos.toByteArray();
        break;
    }
    return signature;
}