1 2 package com.ca.commons.security.asn1; 3 4 import com.ca.commons.cbutil.CBBase64; 5 6 import java.io.*; 7 8 16 public class ASN1Util implements java.io.Serializable  17 { 18 19 private static DERCoder derCoder = new DERCoder(); 20 21 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 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 return object; 52 } 53 54 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 66 String s = null; 67 while ((s = reader.readLine()) != null 68 && !(s.startsWith("-----BEGIN"))) 69 ; 70 71 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 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 105 FileInputStream in = new FileInputStream(f); 106 in.read(buffer); 107 in.close(); 108 return fromByteArray(buffer); 109 } 110 111 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 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 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 173 public static void savePEM(ASN1Object o, File file, String name) 174 throws IOException, ASN1Exception 175 { 176 177 byte [] inPEM = toByteArrayPEM(o); 178 savePEM(inPEM, file, name); 179 } 180 181 185 public static void savePEM(byte [] inPEM, File file, String name) 186 throws IOException 187 { 188 PrintWriter writer = new PrintWriter(new FileWriter(file)); 189 190 191 writer.print("-----BEGIN "); 192 writer.print(name); 193 writer.println("-----"); 194 195 196 int i; 197 for (i = 0; i < inPEM.length / 64; i++) 198 { 199 String temp = new String (inPEM, i*64, 64); 200 writer.println(temp); 201 } 202 if (inPEM.length != i * 64) 203 { 204 String temp = new String (inPEM, i*64, inPEM.length-i*64); 205 writer.println(temp); 206 } 207 208 209 writer.print("-----END "); 210 writer.print(name); 211 writer.println("-----"); 212 writer.close(); 213 } 214 215 221 222 228 } 229 | Popular Tags |