1 17 18 package org.apache.geronimo.util.asn1; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.EOFException ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.util.Vector ; 25 26 29 public class BERInputStream 30 extends DERInputStream 31 { 32 private DERObject END_OF_STREAM = new DERObject() { 33 void encode( 34 DEROutputStream out) 35 throws IOException 36 { 37 throw new IOException ("Eeek!"); 38 } 39 public int hashCode() 40 { 41 return 0; 42 } 43 public boolean equals( 44 Object o) 45 { 46 return o == this; 47 } 48 }; 49 public BERInputStream( 50 InputStream is) 51 { 52 super(is); 53 } 54 55 58 private byte[] readIndefiniteLengthFully() 59 throws IOException 60 { 61 ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 62 int b, b1; 63 64 b1 = read(); 65 66 while ((b = read()) >= 0) 67 { 68 if (b1 == 0 && b == 0) 69 { 70 break; 71 } 72 73 bOut.write(b1); 74 b1 = b; 75 } 76 77 return bOut.toByteArray(); 78 } 79 80 private BERConstructedOctetString buildConstructedOctetString() 81 throws IOException 82 { 83 Vector octs = new Vector (); 84 85 for (;;) 86 { 87 DERObject o = readObject(); 88 89 if (o == END_OF_STREAM) 90 { 91 break; 92 } 93 94 octs.addElement(o); 95 } 96 97 return new BERConstructedOctetString(octs); 98 } 99 100 public DERObject readObject() 101 throws IOException 102 { 103 int tag = read(); 104 if (tag == -1) 105 { 106 throw new EOFException (); 107 } 108 109 int length = readLength(); 110 111 if (length < 0) { 113 switch (tag) 114 { 115 case NULL: 116 return null; 117 case SEQUENCE | CONSTRUCTED: 118 BERConstructedSequence seq = new BERConstructedSequence(); 119 120 for (;;) 121 { 122 DERObject obj = readObject(); 123 124 if (obj == END_OF_STREAM) 125 { 126 break; 127 } 128 129 seq.addObject(obj); 130 } 131 return seq; 132 case OCTET_STRING | CONSTRUCTED: 133 return buildConstructedOctetString(); 134 case SET | CONSTRUCTED: 135 ASN1EncodableVector v = new ASN1EncodableVector(); 136 137 for (;;) 138 { 139 DERObject obj = readObject(); 140 141 if (obj == END_OF_STREAM) 142 { 143 break; 144 } 145 146 v.add(obj); 147 } 148 return new BERSet(v); 149 default: 150 if ((tag & TAGGED) != 0) 154 { 155 if ((tag & 0x1f) == 0x1f) 156 { 157 throw new IOException ("unsupported high tag encountered"); 158 } 159 160 if ((tag & CONSTRUCTED) == 0) 164 { 165 byte[] bytes = readIndefiniteLengthFully(); 166 167 return new BERTaggedObject(false, tag & 0x1f, new DEROctetString(bytes)); 168 } 169 170 DERObject dObj = readObject(); 174 175 if (dObj == END_OF_STREAM) { 177 return new DERTaggedObject(tag & 0x1f); 178 } 179 180 DERObject next = readObject(); 181 182 if (next == END_OF_STREAM) 187 { 188 return new BERTaggedObject(tag & 0x1f, dObj); 189 } 190 191 seq = new BERConstructedSequence(); 195 196 seq.addObject(dObj); 197 198 do 199 { 200 seq.addObject(next); 201 next = readObject(); 202 } 203 while (next != END_OF_STREAM); 204 205 return new BERTaggedObject(false, tag & 0x1f, seq); 206 } 207 208 throw new IOException ("unknown BER object encountered"); 209 } 210 } 211 else 212 { 213 if (tag == 0 && length == 0) { 215 return END_OF_STREAM; 216 } 217 218 byte[] bytes = new byte[length]; 219 220 readFully(bytes); 221 222 return buildObject(tag, bytes); 223 } 224 } 225 } 226 | Popular Tags |