📄 asn1inputstream.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: ASN1InputStream.java
package org.bouncycastle.asn1;
import java.io.*;
import java.util.Vector;
// Referenced classes of package org.bouncycastle.asn1:
// ASN1EncodableVector, BERConstructedOctetString, BERNull, BERSequence,
// BERSet, BERTaggedObject, DERApplicationSpecific, DERBMPString,
// DERBitString, DERBoolean, DEREnumerated, DERGeneralString,
// DERGeneralizedTime, DERIA5String, DERInteger, DERNull,
// DERNumericString, DERObjectIdentifier, DEROctetString, DERPrintableString,
// DERSequence, DERSet, DERT61String, DERTaggedObject,
// DERTags, DERUTCTime, DERUTF8String, DERUniversalString,
// DERUnknownTag, DERVisibleString, DefiniteLengthInputStream, DERObject,
// DEROutputStream
public class ASN1InputStream extends FilterInputStream
implements DERTags
{
private static final DERObject END_OF_STREAM = new DERObject() {
void encode(DEROutputStream out)
throws IOException
{
throw new IOException("Eeek!");
}
public int hashCode()
{
return 0;
}
public boolean equals(Object o)
{
return o == this;
}
};
boolean eofFound;
int limit;
public ASN1InputStream(InputStream is)
{
super(is);
eofFound = false;
limit = 0x7fffffff;
}
public ASN1InputStream(byte input[])
{
this(((InputStream) (new ByteArrayInputStream(input))), input.length);
}
public ASN1InputStream(InputStream input, int limit)
{
super(input);
eofFound = false;
this.limit = 0x7fffffff;
this.limit = limit;
}
protected int readLength()
throws IOException
{
int length = read();
if (length < 0)
throw new IOException("EOF found when length expected");
if (length == 128)
return -1;
if (length > 127)
{
int size = length & 0x7f;
if (size > 4)
throw new IOException("DER length more than 4 bytes");
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;
}
if (length < 0)
throw new IOException("corrupted stream - negative length found");
if (length >= limit)
throw new IOException("corrupted stream - out of bounds length found");
}
return length;
}
protected void readFully(byte bytes[])
throws IOException
{
int left = bytes.length;
if (left == 0)
return;
int len;
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");
else
return;
}
protected DERObject buildObject(int tag, int tagNo, int length)
throws IOException
{
if ((tag & 0x40) != 0)
return new DERApplicationSpecific(tagNo, readDefiniteLengthFully(length));
boolean isConstructed = (tag & 0x20) != 0;
if (isConstructed)
{
switch (tag)
{
case 48: // '0'
return new DERSequence(buildDerEncodableVector(length));
case 49: // '1'
return new DERSet(buildDerEncodableVector(length), false);
case 36: // '$'
return buildDerConstructedOctetString(length);
}
if ((tag & 0x80) != 0)
{
if (length == 0)
return new DERTaggedObject(false, tagNo, new DERSequence());
ASN1EncodableVector v = buildDerEncodableVector(length);
if (v.size() == 1)
return new DERTaggedObject(tagNo, v.get(0));
else
return new DERTaggedObject(false, tagNo, new DERSequence(v));
} else
{
return new DERUnknownTag(tag, readDefiniteLengthFully(length));
}
}
byte bytes[] = readDefiniteLengthFully(length);
switch (tag)
{
case 5: // '\005'
return DERNull.INSTANCE;
case 1: // '\001'
return new DERBoolean(bytes);
case 2: // '\002'
return new DERInteger(bytes);
case 10: // '\n'
return new DEREnumerated(bytes);
case 6: // '\006'
return new DERObjectIdentifier(bytes);
case 3: // '\003'
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 18: // '\022'
return new DERNumericString(bytes);
case 12: // '\f'
return new DERUTF8String(bytes);
case 19: // '\023'
return new DERPrintableString(bytes);
case 22: // '\026'
return new DERIA5String(bytes);
case 20: // '\024'
return new DERT61String(bytes);
case 26: // '\032'
return new DERVisibleString(bytes);
case 27: // '\033'
return new DERGeneralString(bytes);
case 28: // '\034'
return new DERUniversalString(bytes);
case 30: // '\036'
return new DERBMPString(bytes);
case 4: // '\004'
return new DEROctetString(bytes);
case 23: // '\027'
return new DERUTCTime(bytes);
case 24: // '\030'
return new DERGeneralizedTime(bytes);
}
if ((tag & 0x80) != 0)
{
if (bytes.length == 0)
return new DERTaggedObject(false, tagNo, DERNull.INSTANCE);
else
return new DERTaggedObject(false, tagNo, new DEROctetString(bytes));
} else
{
return new DERUnknownTag(tag, bytes);
}
}
private byte[] readDefiniteLengthFully(int length)
throws IOException
{
byte bytes[] = new byte[length];
readFully(bytes);
return bytes;
}
private byte[] readIndefiniteLengthFully()
throws IOException
{
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
int b;
for (int b1 = read(); (b = read()) >= 0 && (b1 != 0 || b != 0); b1 = b)
bOut.write(b1);
return bOut.toByteArray();
}
private BERConstructedOctetString buildConstructedOctetString(DERObject sentinel)
throws IOException
{
Vector octs = new Vector();
DERObject o;
while ((o = readObject()) != sentinel)
octs.addElement(o);
return new BERConstructedOctetString(octs);
}
private BERConstructedOctetString buildDerConstructedOctetString(int length)
throws IOException
{
DefiniteLengthInputStream dIn = new DefiniteLengthInputStream(this, length);
ASN1InputStream aIn = new ASN1InputStream(dIn, length);
return aIn.buildConstructedOctetString(null);
}
private ASN1EncodableVector buildEncodableVector(DERObject sentinel)
throws IOException
{
ASN1EncodableVector v = new ASN1EncodableVector();
DERObject o;
while ((o = readObject()) != sentinel)
v.add(o);
return v;
}
private ASN1EncodableVector buildDerEncodableVector(int length)
throws IOException
{
DefiniteLengthInputStream dIn = new DefiniteLengthInputStream(this, length);
ASN1InputStream aIn = new ASN1InputStream(dIn, length);
return aIn.buildEncodableVector(null);
}
public DERObject readObject()
throws IOException
{
int tag = read();
if (tag == -1)
if (eofFound)
{
throw new EOFException("attempt to read past end of file.");
} else
{
eofFound = true;
return null;
}
int tagNo = 0;
if ((tag & 0x80) != 0 || (tag & 0x40) != 0)
tagNo = readTagNumber(tag);
int length = readLength();
if (length < 0)
{
switch (tag)
{
case 5: // '\005'
return BERNull.INSTANCE;
case 48: // '0'
return new BERSequence(buildEncodableVector(END_OF_STREAM));
case 49: // '1'
return new BERSet(buildEncodableVector(END_OF_STREAM), false);
case 36: // '$'
return buildConstructedOctetString(END_OF_STREAM);
}
if ((tag & 0x80) != 0)
{
if ((tag & 0x20) == 0)
{
byte bytes[] = readIndefiniteLengthFully();
return new BERTaggedObject(false, tagNo, new DEROctetString(bytes));
}
ASN1EncodableVector v = buildEncodableVector(END_OF_STREAM);
if (v.size() == 0)
return new DERTaggedObject(tagNo);
if (v.size() == 1)
return new BERTaggedObject(tagNo, v.get(0));
else
return new BERTaggedObject(false, tagNo, new BERSequence(v));
} else
{
throw new IOException("unknown BER object encountered");
}
}
if (tag == 0 && length == 0)
return END_OF_STREAM;
else
return buildObject(tag, tagNo, length);
}
private int readTagNumber(int tag)
throws IOException
{
int tagNo = tag & 0x1f;
if (tagNo == 31)
{
int b = read();
tagNo = 0;
for (; b >= 0 && (b & 0x80) != 0; b = read())
{
tagNo |= b & 0x7f;
tagNo <<= 7;
}
if (b < 0)
{
eofFound = true;
throw new EOFException("EOF found inside tag value.");
}
tagNo |= b & 0x7f;
}
return tagNo;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -