KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > security > asn1 > ASN1Util


1
2 package com.ca.commons.security.asn1;
3
4 import com.ca.commons.cbutil.CBBase64;
5
6 import java.io.*;
7
8 /**
9  * This class provides some basic methods to convert ASN1Objects
10  * from/to byte arrays and files. The byte arrays can be base64 .
11  * encoded. The files can be in Privacy Enhanced Mail (PEM) format.
12  * They are all assumed not to be encrypted.
13  *
14  * @author who
15  */

16 public class ASN1Util implements java.io.Serializable JavaDoc
17 {
18
19     private static DERCoder derCoder = new DERCoder();
20
21     /**
22      * Creates an ASN1Object from a byte array. The byte array can
23      * be base64 encoded or not.
24      * @exception iss.security.asn1.ASN1Exception if creating the
25      * ASN1Obect from the byte array throws this exception.
26      */

27     public static ASN1Object fromByteArray(byte [] content)
28     throws ASN1Exception
29     {
30         if (content == null || content.length == 0)
31         {
32             return null;
33         }
34
35         ASN1Object object = null;
36         byte [] byteArray;
37
38         /* test whether base64 encoded */
39         byte [] base64Decoded = CBBase64.decode(content);
40         if (base64Decoded != null)
41         {
42             byteArray = base64Decoded;
43         }
44         else
45         {
46             byteArray = content;
47         }
48
49         object = derCoder.decode(byteArray);
50         // object.setByteArray(byteArray);
51
return object;
52     }
53
54     /**
55      * Creates an ASN1Object from a file, while the file may be in binary
56      * form or in PEM format.
57      * @exception iss.security.asn1.ASN1Exception if creating the
58      * ASN1Object from the file throws this exception.
59      */

60     public static ASN1Object fromFile(File f) throws IOException, ASN1Exception
61     {
62         BufferedReader reader = new BufferedReader(new FileReader(f));
63         byte [] buffer = new byte[(int) (f.length())];
64
65         /* checks the PEM file beginning identifier */
66         String JavaDoc s = null;
67         while ((s = reader.readLine()) != null
68                 && !(s.startsWith("-----BEGIN")))
69             ;
70
71         /* file is in PEM format, need to do base64 decoding */
72         if (s != null)
73         {
74             s = reader.readLine();
75             int offset = 0;
76             byte [] temp;
77
78             while (s != null)
79             {
80                 temp = s.getBytes();
81                 System.arraycopy(temp, 0, buffer, offset, temp.length);
82                 offset += temp.length;
83                 s = reader.readLine();
84                 if (s == null)
85                 {
86                     reader.close();
87                     return null;
88                 }
89
90                 /* checks the PEM file ending identifier */
91                 if (s.startsWith("-----END"))
92                 {
93                     s = null;
94                 }
95             }
96             reader.close();
97             byte [] result = new byte[offset];
98             System.arraycopy(buffer, 0, result, 0, offset);
99             return fromByteArray(result);
100         }
101
102         reader.close();
103
104         /* file is in binary format, need not to do base64 decoding */
105         FileInputStream in = new FileInputStream(f);
106         in.read(buffer);
107         in.close();
108         return fromByteArray(buffer);
109     }
110
111     /**
112      * Gets the DER encoded byte array of the given ASN1Object.
113      * @exception iss.security.asn1.ASN1Exception if the DER encoding
114      * process throws the exception.
115      */

116     public static byte [] toByteArrayDER(ASN1Object o)
117     throws ASN1Exception
118     {
119         if (o.getByteArray() == null)
120         {
121             byte [] data = derCoder.encode(o);
122             o.setByteArray(data);
123             return data;
124         }
125         else
126         {
127             return o.getByteArray();
128         }
129     }
130
131     /**
132      * Gets the PEM (DER and base64 encoded) byte array of the given
133      * ASN1Object.
134      * @exception iss.security.asn1.ASN1Exception if the DER encoding
135      * process throws the exception.
136      */

137     public static byte [] toByteArrayPEM(ASN1Object o)
138     throws ASN1Exception
139     {
140         if (o.getByteArray() == null)
141         {
142             byte [] data = derCoder.encode(o);
143             o.setByteArray(data);
144             return CBBase64.encode(data);
145         }
146         else
147         {
148             return CBBase64.encode(o.getByteArray());
149         }
150     }
151
152     /**
153      * Gets the DER encoded byte array of the given ASN1Object, and
154      * saves the byte array to a file.
155      * @exception iss.security.asn1.ASN1Exception if the DER encoding
156      * process throws this exception.
157      */

158     public static void saveDER(ASN1Object o, File file)
159     throws IOException, ASN1Exception
160     {
161         byte [] data = toByteArrayDER(o);
162         FileOutputStream out = new FileOutputStream(file);
163         out.write(data);
164         out.close();
165     }
166
167     /**
168      * Saves the given ASN1Object to a file in PEM format.
169      * @param name the name/type of the ASN1Object.
170      * @exception iss.security.asn1.ASN1Exception if the DER encoding
171      * process throws this exception.
172      */

173     public static void savePEM(ASN1Object o, File file, String JavaDoc name)
174     throws IOException, ASN1Exception
175     {
176
177         byte [] inPEM = toByteArrayPEM(o);
178         savePEM(inPEM, file, name);
179     }
180
181     /**
182      * Saves the given Object ASN.1 encoding to a file in PEM format.
183      * @param name - the name/type of the ASN1Object.
184      */

185     public static void savePEM(byte [] inPEM, File file, String JavaDoc name)
186     throws IOException
187     {
188         PrintWriter writer = new PrintWriter(new FileWriter(file));
189
190         /* write beginning line */
191         writer.print("-----BEGIN ");
192         writer.print(name);
193         writer.println("-----");
194
195         /* write data */
196         int i;
197         for (i = 0; i < inPEM.length / 64; i++)
198         {
199             String JavaDoc temp = new String JavaDoc(inPEM, i*64, 64);
200             writer.println(temp);
201         }
202         if (inPEM.length != i * 64)
203         {
204             String JavaDoc temp = new String JavaDoc(inPEM, i*64, inPEM.length-i*64);
205             writer.println(temp);
206         }
207
208         /* write ending line */
209         writer.print("-----END ");
210         writer.print(name);
211         writer.println("-----");
212         writer.close();
213     }
214
215     /**
216      * Base64 encoding of the input byte array.
217     public static byte [] der2pem(byte [] data) {
218         return Base64Coder.encode(data);
219 }
220      */

221
222     /**
223      * Base64 decoding of the input byte array.
224     public static byte [] pem2der(byte [] data) {
225         return Base64Coder.decode(data);
226 }
227      */

228 }
229
Popular Tags