1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
package com.lowagie.bc.asn1;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
/**
* a general purpose ASN.1 decoder - note: this class differs from the
* others in that it returns null after it has read the last object in
* the stream. If an ASN.1 NULL is encountered a DER/BER Null object is
* returned.
*/
public class ASN1InputStream
extends DERInputStream
{
private DERObject END_OF_STREAM = new DERObject() {
void encode(
DEROutputStream out)
throws IOException
{
throw new IOException("Eeek!");
}
};
boolean eofFound = false;
public ASN1InputStream(
InputStream is)
{
super(is);
}
protected int readLength()
throws IOException
{
int length = read();
if (length < 0)
{
throw new IOException("EOF found when length expected");
}
if (length == 0x80)
{
return -1; // indefinite-length encoding
}
if (length > 127)
{
int size = length & 0x7f;
length = 0;
for (int i = 0; i < size; i++)
{
int next = read();
if (next < 0)
{
throw new IOException("EOF found reading length");
}
length = (length << 8) + next;
}
}
return length;
}
protected void readFully(
byte[] bytes)
throws IOException
{
int left = bytes.length;
int len;
if (left == 0)
{
return;
}
while ((len = read(bytes, bytes.length - left, left)) > 0)
{
if ((left -= len) == 0)
{
return;
}
}
if (left != 0)
{
throw new EOFException("EOF encountered in middle of object");
}
}
/**
* build an object given its tag and a byte stream to construct it
* from.
*/
protected DERObject buildObject(
int tag,
byte[] bytes)
throws IOException
{
if ((tag & APPLICATION) != 0)
{
return new DERApplicationSpecific(tag, bytes);
}
switch (tag)
{
case NULL:
return new DERNull();
case SEQUENCE | CONSTRUCTED:
ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
ASN1InputStream aIn = new ASN1InputStream(bIn);
ASN1EncodableVector v = new ASN1EncodableVector();
DERObject obj = aIn.readObject();
while (obj != null)
{
v.add(obj);
obj = aIn.readObject();
}
return new DERSequence(v);
case SET | CONSTRUCTED:
bIn = new ByteArrayInputStream(bytes);
aIn = new ASN1InputStream(bIn);
v = new ASN1EncodableVector();
obj = aIn.readObject();
while (obj != null)
{
v.add(obj);
obj = aIn.readObject();
}
return new DERSet(v);
case BOOLEAN:
return new DERBoolean(bytes);
case INTEGER:
return new DERInteger(bytes);
case ENUMERATED:
return new DEREnumerated(bytes);
case OBJECT_IDENTIFIER:
return new DERObjectIdentifier(bytes);
case BIT_STRING:
int padBits = bytes[0];
byte[] data = new byte[bytes.length - 1];
System.arraycopy(bytes, 1, data, 0, bytes.length - 1);
return new DERBitString(data, padBits);
case UTF8_STRING:
return new DERUTF8String(bytes);
case PRINTABLE_STRING:
return new DERPrintableString(bytes);
case IA5_STRING:
return new DERIA5String(bytes);
case T61_STRING:
return new DERT61String(bytes);
case VISIBLE_STRING:
return new DERVisibleString(bytes);
case GENERAL_STRING:
return new DERGeneralString(bytes);
case UNIVERSAL_STRING:
return new DERUniversalString(bytes);
case BMP_STRING:
return new DERBMPString(bytes);
case OCTET_STRING:
return new DEROctetString(bytes);
case UTC_TIME:
return new DERUTCTime(bytes);
case GENERALIZED_TIME:
return new DERGeneralizedTime(bytes);
default:
//
// with tagged object tag number is bottom 5 bits
//
if ((tag & TAGGED) != 0)
{
int tagNo = tag & 0x1f;
if (tagNo == 0x1f)
{
int idx = 0;
tagNo = 0;
while ((bytes[idx] & 0x80) != 0)
{
tagNo |= (bytes[idx++] & 0x7f);
tagNo <<= 7;
}
tagNo |= (bytes[idx] & 0x7f);
byte[] tmp = bytes;
bytes = new byte[tmp.length - (idx + 1)];
System.arraycopy(tmp, idx + 1, bytes, 0, bytes.length);
}
if (bytes.length == 0) // empty tag!
{
if ((tag & CONSTRUCTED) == 0)
{
return new DERTaggedObject(false, tagNo, new DERNull());
}
else
{
return new DERTaggedObject(false, tagNo, new DERSequence());
}
}
//
// simple type - implicit... return an octet string
//
if ((tag & CONSTRUCTED) == 0)
{
return new DERTaggedObject(false, tagNo, new DEROctetString(bytes));
}
bIn = new ByteArrayInputStream(bytes);
aIn = new ASN1InputStream(bIn);
DEREncodable dObj = aIn.readObject();
//
// explicitly tagged (probably!) - if it isn't we'd have to
// tell from the context
//
if (aIn.available() == 0)
{
return new DERTaggedObject(tagNo, dObj);
}
//
// another implicit object, we'll create a sequence...
//
v = new ASN1EncodableVector();
while (dObj != null)
{
v.add(dObj);
dObj = aIn.readObject();
}
return new DERTaggedObject(false, tagNo, new DERSequence(v));
}
return new DERUnknownTag(tag, bytes);
}
}
/**
* read a string of bytes representing an indefinite length object.
*/
private byte[] readIndefiniteLengthFully()
throws IOException
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int b, b1;
b1 = read();
while ((b = read()) >= 0)
{
if (b1 == 0 && b == 0)
{
break;
}
bOut.write(b1);
b1 = b;
}
return bOut.toByteArray();
}
private BERConstructedOctetString buildConstructedOctetString()
throws IOException
{
Vector octs = new Vector();
for (;;)
{
DERObject o = readObject();
if (o == END_OF_STREAM)
{
break;
}
octs.addElement(o);
}
return new BERConstructedOctetString(octs);
}
public DERObject readObject()
throws IOException
{
int tag = read();
if (tag == -1)
{
if (eofFound)
{
throw new EOFException("attempt to read past end of file.");
}
eofFound = true;
return null;
}
int length = readLength();
if (length < 0) // indefinite length method
{
switch (tag)
{
case NULL:
return new BERNull();
case SEQUENCE | CONSTRUCTED:
ASN1EncodableVector v = new ASN1EncodableVector();
for (;;)
{
DERObject obj = readObject();
if (obj == END_OF_STREAM)
{
break;
}
v.add(obj);
}
return new BERSequence(v);
case SET | CONSTRUCTED:
v = new ASN1EncodableVector();
for (;;)
{
DERObject obj = readObject();
if (obj == END_OF_STREAM)
{
break;
}
v.add(obj);
}
return new BERSet(v);
case OCTET_STRING | CONSTRUCTED:
return buildConstructedOctetString();
default:
//
// with tagged object tag number is bottom 5 bits
//
if ((tag & TAGGED) != 0)
{
int tagNo = tag & 0x1f;
if (tagNo == 0x1f)
{
int b = read();
tagNo = 0;
while ((b >= 0) && ((b & 0x80) != 0))
{
tagNo |= (b & 0x7f);
tagNo <<= 7;
b = read();
}
tagNo |= (b & 0x7f);
}
//
// simple type - implicit... return an octet string
//
if ((tag & CONSTRUCTED) == 0)
{
byte[] bytes = readIndefiniteLengthFully();
return new BERTaggedObject(false, tagNo, new DEROctetString(bytes));
}
//
// either constructed or explicitly tagged
//
DERObject dObj = readObject();
if (dObj == END_OF_STREAM) // empty tag!
{
return new DERTaggedObject(tagNo);
}
DERObject next = readObject();
//
// explicitly tagged (probably!) - if it isn't we'd have to
// tell from the context
//
if (next == END_OF_STREAM)
{
return new BERTaggedObject(tagNo, dObj);
}
//
// another implicit object, we'll create a sequence...
//
v = new ASN1EncodableVector();
v.add(dObj);
do
{
v.add(next);
next = readObject();
}
while (next != END_OF_STREAM);
return new BERTaggedObject(false, tagNo, new BERSequence(v));
}
throw new IOException("unknown BER object encountered");
}
}
else
{
if (tag == 0 && length == 0) // end of contents marker.
{
return END_OF_STREAM;
}
byte[] bytes = new byte[length];
readFully(bytes);
return buildObject(tag, bytes);
}
}
}
|