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
|
/* see bouncycastle_license.txt */
package com.lowagie.bc.asn1;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Don't use this class. It will eventually disappear, use ASN1InputStream.
* <br>
* This class is scheduled for removal.
*/
public class DERInputStream
extends FilterInputStream implements DERTags
{
public DERInputStream(
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;
if (left == 0)
{
return;
}
while (left > 0)
{
int l = read(bytes, bytes.length - left, left);
if (l < 0)
{
throw new EOFException("unexpected end of stream");
}
left -= l;
}
}
/**
* build an object given its tag and a byte stream to construct it
* from.
*/
protected DERObject buildObject(
int tag,
byte[] bytes)
throws IOException
{
switch (tag)
{
case NULL:
return null;
case SEQUENCE | CONSTRUCTED:
ByteArrayInputStream bIn = new ByteArrayInputStream(bytes);
BERInputStream dIn = new BERInputStream(bIn);
DERConstructedSequence seq = new DERConstructedSequence();
try
{
for (;;)
{
DERObject obj = dIn.readObject();
seq.addObject(obj);
}
}
catch (EOFException ex)
{
return seq;
}
case SET | CONSTRUCTED:
bIn = new ByteArrayInputStream(bytes);
dIn = new BERInputStream(bIn);
ASN1EncodableVector v = new ASN1EncodableVector();
try
{
for (;;)
{
DERObject obj = dIn.readObject();
v.add(obj);
}
}
catch (EOFException ex)
{
return new DERConstructedSet(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 UNIVERSAL_STRING:
return new DERUniversalString(bytes);
case GENERAL_STRING:
return new DERGeneralString(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)
{
if ((tag & 0x1f) == 0x1f)
{
throw new IOException("unsupported high tag encountered");
}
if (bytes.length == 0) // empty tag!
{
if ((tag & CONSTRUCTED) == 0)
{
return new DERTaggedObject(false, tag & 0x1f, new DERNull());
}
else
{
return new DERTaggedObject(false, tag & 0x1f, new DERConstructedSequence());
}
}
//
// simple type - implicit... return an octet string
//
if ((tag & CONSTRUCTED) == 0)
{
return new DERTaggedObject(false, tag & 0x1f, new DEROctetString(bytes));
}
bIn = new ByteArrayInputStream(bytes);
dIn = new BERInputStream(bIn);
DEREncodable dObj = dIn.readObject();
//
// explicitly tagged (probably!) - if it isn't we'd have to
// tell from the context
//
if (dIn.available() == 0)
{
return new DERTaggedObject(tag & 0x1f, dObj);
}
//
// another implicit object, we'll create a sequence...
//
seq = new DERConstructedSequence();
seq.addObject(dObj);
try
{
for (;;)
{
dObj = dIn.readObject();
seq.addObject(dObj);
}
}
catch (EOFException ex)
{
// ignore --
}
return new DERTaggedObject(false, tag & 0x1f, seq);
}
return new DERUnknownTag(tag, bytes);
}
}
public DERObject readObject()
throws IOException
{
int tag = read();
if (tag == -1)
{
throw new EOFException();
}
int length = readLength();
byte[] bytes = new byte[length];
readFully(bytes);
return buildObject(tag, bytes);
}
}
|