Search in sources :

Example 1 with PemWriter

use of com.github.zhenwei.core.util.io.pem.PemWriter in project LinLong-Java by zhenwei1108.

the class PKIXCertPath method getEncoded.

/**
 * Returns the encoded form of this certification path, using the specified encoding.
 *
 * @param encoding the name of the encoding to use
 * @return the encoded bytes
 * @throws CertificateEncodingException if an encoding error occurs or the encoding requested is
 *                                      not supported
 */
public byte[] getEncoded(String encoding) throws CertificateEncodingException {
    if (encoding.equalsIgnoreCase("PkiPath")) {
        ASN1EncodableVector v = new ASN1EncodableVector();
        ListIterator iter = certificates.listIterator(certificates.size());
        while (iter.hasPrevious()) {
            v.add(toASN1Object((X509Certificate) iter.previous()));
        }
        return toDEREncoded(new DERSequence(v));
    } else if (encoding.equalsIgnoreCase("PKCS7")) {
        ContentInfo encInfo = new ContentInfo(PKCSObjectIdentifiers.data, null);
        ASN1EncodableVector v = new ASN1EncodableVector();
        for (int i = 0; i != certificates.size(); i++) {
            v.add(toASN1Object((X509Certificate) certificates.get(i)));
        }
        SignedData sd = new SignedData(new ASN1Integer(1), new DERSet(), encInfo, new DERSet(v), null, new DERSet());
        return toDEREncoded(new ContentInfo(PKCSObjectIdentifiers.signedData, sd));
    } else if (encoding.equalsIgnoreCase("PEM")) {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        PemWriter pWrt = new PemWriter(new OutputStreamWriter(bOut));
        try {
            for (int i = 0; i != certificates.size(); i++) {
                pWrt.writeObject(new PemObject("CERTIFICATE", ((X509Certificate) certificates.get(i)).getEncoded()));
            }
            pWrt.close();
        } catch (Exception e) {
            throw new CertificateEncodingException("can't encode certificate for PEM encoded path");
        }
        return bOut.toByteArray();
    } else {
        throw new CertificateEncodingException("unsupported encoding: " + encoding);
    }
}
Also used : SignedData(com.github.zhenwei.core.asn1.pkcs.SignedData) PemWriter(com.github.zhenwei.core.util.io.pem.PemWriter) CertificateEncodingException(java.security.cert.CertificateEncodingException) ASN1Integer(com.github.zhenwei.core.asn1.ASN1Integer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ListIterator(java.util.ListIterator) DERSet(com.github.zhenwei.core.asn1.DERSet) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException) CertificateEncodingException(java.security.cert.CertificateEncodingException) PemObject(com.github.zhenwei.core.util.io.pem.PemObject) DERSequence(com.github.zhenwei.core.asn1.DERSequence) ContentInfo(com.github.zhenwei.core.asn1.pkcs.ContentInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)1 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)1 DERSequence (com.github.zhenwei.core.asn1.DERSequence)1 DERSet (com.github.zhenwei.core.asn1.DERSet)1 ContentInfo (com.github.zhenwei.core.asn1.pkcs.ContentInfo)1 SignedData (com.github.zhenwei.core.asn1.pkcs.SignedData)1 PemObject (com.github.zhenwei.core.util.io.pem.PemObject)1 PemWriter (com.github.zhenwei.core.util.io.pem.PemWriter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 NoSuchProviderException (java.security.NoSuchProviderException)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 ListIterator (java.util.ListIterator)1