Search in sources :

Example 1 with DEREncoder

use of codec.asn1.DEREncoder in project core by jcryptool.

the class ImportExportManager method exportKeyPair.

public void exportKeyPair(IPath path, PrivateKey key, Certificate[] chain, char[] password) {
    PFX pfx;
    X509Certificate[] x509Chain = convert(chain);
    try {
        if (x509Chain.length > 1) {
            X509Certificate[] shortChain = new X509Certificate[x509Chain.length - 1];
            for (int i = 1; i < chain.length; i++) {
                shortChain[i - 1] = x509Chain[i];
            }
            pfx = new PFX(key, x509Chain[0], shortChain, password, null, null);
        } else {
            pfx = new PFX(key, x509Chain[0], null, password, null, null);
        }
        IFileStore fileStore = EFS.getStore(URIUtil.toURI(path));
        OutputStream os = new BufferedOutputStream(fileStore.openOutputStream(EFS.APPEND, null));
        DEREncoder encoder = new DEREncoder(os);
        pfx.encode(encoder);
        encoder.close();
        os.close();
    } catch (CertificateEncodingException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CertificateEncodingException while creating a PFX", e, true);
    } catch (GeneralSecurityException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "GeneralSecurityException while creating a PFX", e, true);
    } catch (ASN1Exception e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "ASN1Exception while creating a PFX", e, true);
    } catch (IOException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "IOException while creating a PFX", e, true);
    } catch (CoreException e) {
        LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "CoreException while creating a PFX", e, true);
    }
}
Also used : PFX(codec.pkcs12.PFX) ASN1Exception(codec.asn1.ASN1Exception) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) GeneralSecurityException(java.security.GeneralSecurityException) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) CoreException(org.eclipse.core.runtime.CoreException) DEREncoder(codec.asn1.DEREncoder) IFileStore(org.eclipse.core.filesystem.IFileStore) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

ASN1Exception (codec.asn1.ASN1Exception)1 DEREncoder (codec.asn1.DEREncoder)1 PFX (codec.pkcs12.PFX)1 BufferedOutputStream (java.io.BufferedOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 OutputStream (java.io.OutputStream)1 GeneralSecurityException (java.security.GeneralSecurityException)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 X509Certificate (java.security.cert.X509Certificate)1 IFileStore (org.eclipse.core.filesystem.IFileStore)1 CoreException (org.eclipse.core.runtime.CoreException)1