Home | History | Annotate | Download | only in openssl
      1 package org.bouncycastle.openssl;
      2 
      3 import java.io.IOException;
      4 import java.io.Writer;
      5 import java.security.NoSuchProviderException;
      6 import java.security.SecureRandom;
      7 
      8 import org.bouncycastle.util.io.pem.PemGenerationException;
      9 import org.bouncycastle.util.io.pem.PemObjectGenerator;
     10 import org.bouncycastle.util.io.pem.PemWriter;
     11 
     12 /**
     13  * General purpose writer for OpenSSL PEM objects.
     14  */
     15 public class PEMWriter
     16     extends PemWriter
     17 {
     18     private String provider;
     19 
     20     /**
     21      * Base constructor.
     22      *
     23      * @param out output stream to use.
     24      */
     25     public PEMWriter(Writer out)
     26     {
     27         this(out, "BC");
     28     }
     29 
     30     public PEMWriter(
     31         Writer  out,
     32         String  provider)
     33     {
     34         super(out);
     35 
     36         this.provider = provider;
     37     }
     38 
     39     public void writeObject(
     40         Object  obj)
     41         throws IOException
     42     {
     43         try
     44         {
     45             super.writeObject(new MiscPEMGenerator(obj));
     46         }
     47         catch (PemGenerationException e)
     48         {
     49             if (e.getCause() instanceof IOException)
     50             {
     51                 throw (IOException)e.getCause();
     52             }
     53 
     54             throw e;
     55         }
     56     }
     57 
     58     public void writeObject(
     59         PemObjectGenerator obj)
     60         throws IOException
     61     {
     62         super.writeObject(obj);
     63     }
     64 
     65     public void writeObject(
     66         Object       obj,
     67         String       algorithm,
     68         char[]       password,
     69         SecureRandom random)
     70         throws IOException
     71     {
     72         try
     73         {
     74             super.writeObject(new MiscPEMGenerator(obj, algorithm, password, random, provider));
     75         }
     76         catch (NoSuchProviderException e)
     77         {
     78             throw new EncryptionException(e.getMessage(), e);
     79         }
     80     }
     81 }
     82