1 2 package com.ca.commons.security.asn1; 3 4 5 import com.ca.commons.cbutil.CBParse; 6 7 import java.util.Vector ; 8 import java.util.Hashtable ; 9 import java.io.*; 10 11 35 public class ASN1Object implements java.io.Serializable  36 { 37 38 39 protected ASN1Type asn1Type; 40 41 42 protected byte [] byteArray; 43 44 45 private static Hashtable asn1ToJava = new Hashtable (27); 46 47 static{ 48 Class p = (new Primitive()).getClass(); 49 register(ASN1Type.BOOLEAN, p); 50 register(ASN1Type.INTEGER, p); 51 register(ASN1Type.OCTET_STRING, p); 52 register(ASN1Type.NULL, p); 53 register(ASN1Type.OBJECT_ID, p); 54 register(ASN1Type.BIT_STRING, p); 55 register(ASN1Type.IA5String, p); 56 register(ASN1Type.T61String, p); 57 register(ASN1Type.PrintableString, p); 58 register(ASN1Type.UTCTime, p); 59 60 register(ASN1Type.GENERALIZEDTIME,p); 61 register(ASN1Type.ENUMERATED,p); 62 register(ASN1Type.UniversalString, p); 63 register(ASN1Type.BMPString, p); 64 65 Class s = (new Sequence()).getClass(); 66 register(ASN1Type.SEQUENCE, s); 67 register(ASN1Type.SET, s); 68 69 Class c = new Context().getClass(); 70 register(ASN1Type.ContextSpecific, c); 71 } 72 73 76 public ASN1Object() 77 {} 78 79 82 protected void init(ASN1Type type) 83 { 84 asn1Type = type; 85 byteArray = null; 86 } 87 88 91 public ASN1Type getASN1Type() 92 { 93 return asn1Type; 94 } 95 96 99 public byte [] getByteArray() 100 { 101 return byteArray; 102 } 103 104 108 void setByteArray(byte [] b) 109 { 110 byteArray = b; 111 } 112 113 116 public boolean isASN1Type(ASN1Type type) 117 { 118 return asn1Type.equals(type); 119 } 120 121 125 public boolean compByteArray(byte [] x, int xoff, byte [] y, 126 int yoff, int len) 127 { 128 if (len <= 0 || x == null || xoff < 0 || x.length < xoff + len 129 || y == null || yoff < 0 || y.length < yoff + len) 130 { 131 return false; 132 } 133 for (int i = 0; i < len; i++) 134 { 135 if (x[xoff+i] != y[yoff+i]) 136 { 137 return false; 138 } 139 } 140 return true; 141 } 142 143 146 public boolean compByteArray(byte [] x, byte [] y) 147 { 148 if (x == null || y == null || x.length != y.length) 149 { 150 return false; 151 } 152 return compByteArray(x, 0, y, 0, x.length); 153 } 154 155 158 public boolean equals(Object o) 159 { 160 if (!(o instanceof com.ca.commons.security.asn1.ASN1Object)) 161 { 162 return false; 163 } 164 try 165 { 166 byte [] der = ASN1Util.toByteArrayDER(this); 167 byte [] der1 = ASN1Util.toByteArrayDER((ASN1Object) o); 168 return compByteArray(der, der1); 169 } 170 catch(ASN1Exception asn1e) 171 { 172 asn1e.printStackTrace(System.out); 173 return false; 174 } 175 } 176 177 180 public String toString() 181 { 182 return asn1Type.toString(); 183 } 184 185 186 187 188 private Object o = null; 189 190 195 public Object getValue() 196 { 197 return o; 198 } 200 201 206 public void setValue(Object o) 207 { 208 this.o = o; 209 } 211 212 213 214 215 Vector comps = new Vector (); 216 217 222 public void addComponent(ASN1Object o) 223 { 224 comps.addElement(o); 225 } 227 228 233 public void addComponent(ASN1Object o, int index) 234 { 235 throw new IllegalArgumentException ("method not supported"); 236 } 237 238 243 public ASN1Object getComponent() 244 { 245 throw new IllegalArgumentException ("method not supported"); 246 } 247 248 253 public ASN1Object getComponent(int index) 254 { 255 return (ASN1Object) comps.elementAt(index); 256 } 258 259 264 public int size() 265 { 266 return comps.size(); 267 } 269 270 271 272 273 278 public boolean implicit() 279 { 280 throw new IllegalArgumentException ("method not supported"); 281 } 282 283 288 public int getTag() 289 { 290 if (asn1Type != null) 291 return asn1Type.getTag(); 292 else 293 throw new IllegalArgumentException ("Object not initialised, does not have a type"); 294 } 295 296 297 298 299 302 private static void register(ASN1Type type, Class c) 303 { 304 asn1ToJava.put(type, c); 305 } 306 307 314 public static ASN1Object create(ASN1Type type) 315 throws ASN1Exception 316 { 317 Class impl = null; 318 try 319 { 320 ASN1Object o; 321 impl = (Class ) asn1ToJava.get(type); 322 if (impl == null) 323 { 324 throw new ASN1Exception(type.toString()+ 325 " : no implementation class available"); 326 } 327 o = (ASN1Object) impl.newInstance(); 328 o.init(type); 329 return o; 330 } 331 catch (InstantiationException e) 332 { 333 throw new ASN1Exception("Cannot create instance for " + 334 type.toString() + "\n" + e.toString()); 335 } 336 catch (IllegalAccessException e) 337 { 338 throw new ASN1Exception("Cannot create instance for " + 339 type.toString() + "\n" + e.toString()); 340 } 341 } 342 343 348 public static ASN1Object create(ASN1Type type, Object v) 349 throws ASN1Exception 350 { 351 ASN1Object o = create(type); 352 o.setValue(v); 353 return o; 354 } 355 356 357 363 364 367 public void initByteArray() 368 { 369 try 370 { 371 byte [] buf = ASN1Util.toByteArrayDER(this); 372 setByteArray(buf); 373 } 374 catch(ASN1Exception asn1e) 375 { 376 asn1e.printStackTrace(System.out); 377 setByteArray(null); 378 } 379 } 380 381 384 public byte [] toDERBytes() 385 { 386 try 387 { 388 return ASN1Util.toByteArrayDER(this); 389 } 390 catch(ASN1Exception asn1e) 391 { 392 asn1e.printStackTrace(System.out); 393 return null; 394 } 395 } 396 397 411 412 415 public boolean toDERFile(File file) 416 throws IOException 417 { 418 try 419 { 420 ASN1Util.saveDER(this, file); 421 return true; 422 } 423 catch(ASN1Exception asn1e) 424 { 425 asn1e.printStackTrace(System.out); 426 return false; 427 } 428 } 429 430 433 public byte [] toBase64() 434 { 435 try 436 { 437 return ASN1Util.toByteArrayPEM(this); 438 } 439 catch(ASN1Exception asn1e) 440 { 441 asn1e.printStackTrace(System.out); 442 return null; 443 } 444 } 445 446 466 467 471 public boolean toPEMFile(File file, String name) 472 throws IOException 473 { 474 try 475 { 476 ASN1Util.savePEM(this, file, name); 477 return true; 478 } 479 catch(ASN1Exception asn1e) 480 { 481 asn1e.printStackTrace(System.out); 482 return false; 483 } 484 } 485 486 500 501 531 532 533 536 537 559 560 564 public static ASN1Object fromBytes(byte [] data) 565 { 566 try 567 { 568 return ASN1Util.fromByteArray(data); 571 } 572 catch(ASN1Exception asn1e) 573 { 574 System.out.println(CBParse.bytes2Hex(data)); 575 asn1e.printStackTrace(System.out); 576 return null; 577 } 578 } 579 580 583 public static ASN1Object fromFile(File file) 584 throws IOException 585 { 586 596 try 597 { 598 return ASN1Util.fromFile(file); 599 } 600 catch(ASN1Exception asn1e) 601 { 602 asn1e.printStackTrace(System.out); 603 return null; 604 } 605 } 608 609 626 627 665 } 666 | Popular Tags |