org.demac.impl.j2me.process.base.service.webservices.asn1
Class DERDecoder

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by org.demac.impl.j2me.process.base.service.webservices.asn1.DERDecoder
All Implemented Interfaces:
java.io.Closeable, Decoder
Direct Known Subclasses:
BERDecoder

public class DERDecoder
extends java.io.FilterInputStream
implements Decoder


Constructor Summary
DERDecoder(java.io.InputStream in)
          Creates an instance that reads from the given input stream.
DERDecoder(java.io.InputStream in, int limit)
          Creates an instance that reads from the given input stream, and enforces the given maximum number of input octets to the decoder.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from the underlying stream without blocking.
 void close()
          This method closes the underlying stream.
static java.lang.Class getClass(int tag)
          Returns the class that is representing the ASN.1 type with the given tag.
 int getInputLimit()
           
 boolean isBody()
           
 boolean isDebug()
           
 void mark(int readAheadLimit)
          This method calls the corresponding method of the underlying stream and also marks this stream's position, which denotes the overall number of octets already read from this stream.
 boolean markSupported()
          Returns true iff the underlying stream supports marking of stream positions.
 int read()
          This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually read.
 int read(byte[] b)
          This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually read.
 int read(byte[] b, int off, int len)
          This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually read.
 int readBase128()
          Reads a base 128 number from this input stream.
 int readBase256(int num)
          This method reads in an decodes a number in base 256 format.
 void readBitString(ASN1BitString t)
           
 void readBoolean(ASN1Boolean t)
           
 void readChoice(ASN1Choice t)
          Reads an ASN.1 CHOICE type.
 void readCollection(ASN1Collection t)
           
 void readCollectionOf(ASN1CollectionOf t)
           
 ASN1Sequence readContent()
           
 ASN1Sequence readIdentifier()
           
 void readInteger(ASN1Integer t)
           
 boolean readNext()
          Reads the next identifier and length octets from the stream and decodes them if skip_ is false.
 void readNull(ASN1Null t)
           
 void readObjectIdentifier(ASN1ObjectIdentifier t)
           
 void readOctetString(ASN1OctetString t)
           
 void readReal(ASN1Real t)
           
 void readString(ASN1String t)
           
 void readTaggedType(ASN1TaggedType t)
          This method also reads in ASN1Opaque types.
 void readTime(ASN1Time t)
           
 ASN1Type readType()
          Reads the next ASN.1 type in the stream.
 void readType(ASN1Type t)
           
 void reset()
          Resets the unerlying input stream to the last marked position and also resets this stream's position to the last marked one.
 void setDebug(boolean debug)
           
 void setInputLimit(int limit)
          Sets the maximum number of input octets read from the underlying stream.
 long skip(long n)
          This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually skipped.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DERDecoder

public DERDecoder(java.io.InputStream in)
Creates an instance that reads from the given input stream.

Parameters:
in - The input stream to read from.

DERDecoder

public DERDecoder(java.io.InputStream in,
                  int limit)
Creates an instance that reads from the given input stream, and enforces the given maximum number of input octets to the decoder.

Parameters:
in - The input stream to read from.
limit - The maximum number of octets allowed in the input stream. A value of 0 disables checking of upper limits.
Method Detail

getClass

public static java.lang.Class getClass(int tag)
                                throws ASN1Exception
Returns the class that is representing the ASN.1 type with the given tag. If no class is known for this tag then an exception is thrown.

Parameters:
tag - The ASN.1 tag of the class to be returned.
Returns:
The class implementing the ASN.1 type with the given tag.
Throws:
ASN1Exception - if no implementing class is known for the given tag.
java.lang.IllegalArgumentException - if the given tag is negative.

setInputLimit

public void setInputLimit(int limit)
Sets the maximum number of input octets read from the underlying stream. A value of 0 disables limits checking. If a limit is set and the decoder detects that the length of a read type wil exceed the limit then an exception will be thrown.

Setting limits prevents DoS attacks on cryptographic services that handle ASN.1/DER/BER input. Such code might be tricked into allocating insane amounts of buffer memory to decode a structure with forged length octets, which might result in out of memory errors. Such code should enforce a reasonable upper bound on the size of processed input structures.

Parameters:
limit - The maximum number of octets allowed when readinginput.

getInputLimit

public int getInputLimit()
Returns:
The maximum number of octets the decoded structures are allowed to have.

setDebug

public void setDebug(boolean debug)

isDebug

public boolean isDebug()

readNext

public boolean readNext()
                 throws ASN1Exception,
                        java.io.IOException
Reads the next identifier and length octets from the stream and decodes them if skip_ is false.

The resulting values are written to tag_, tagclass_, primitive_, length_, and indefinite_.

If the stream is at its end then false is returned and true else. If skip_ is true then nothing is read and true is returned.

This method does not enforce DER, it can be used to parse BER as well. In other words, calling methods have to check for indefinite length encodings.

Returns:
true if the next header was read.
Throws:
ASN1Exception
java.io.IOException

readType

public ASN1Type readType()
                  throws ASN1Exception,
                         java.io.IOException
Reads the next ASN.1 type in the stream. If the next type is end-of-code (EOC) then null is returned. EOC marks the end of indefinite length encodings.

Specified by:
readType in interface Decoder
Returns:
The ASN.1 type read from the stream or null if the type is EOC.
Throws:
ASN1Exception
java.io.IOException

readType

public void readType(ASN1Type t)
              throws ASN1Exception,
                     java.io.IOException
Specified by:
readType in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readBoolean

public void readBoolean(ASN1Boolean t)
                 throws ASN1Exception,
                        java.io.IOException
Specified by:
readBoolean in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readInteger

public void readInteger(ASN1Integer t)
                 throws ASN1Exception,
                        java.io.IOException
Specified by:
readInteger in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readBitString

public void readBitString(ASN1BitString t)
                   throws ASN1Exception,
                          java.io.IOException
Specified by:
readBitString in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readOctetString

public void readOctetString(ASN1OctetString t)
                     throws ASN1Exception,
                            java.io.IOException
Specified by:
readOctetString in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readNull

public void readNull(ASN1Null t)
              throws ASN1Exception,
                     java.io.IOException
Specified by:
readNull in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readObjectIdentifier

public void readObjectIdentifier(ASN1ObjectIdentifier t)
                          throws ASN1Exception,
                                 java.io.IOException
Specified by:
readObjectIdentifier in interface Decoder
Throws:
ASN1Exception
java.io.IOException

isBody

public boolean isBody()

readIdentifier

public ASN1Sequence readIdentifier()

readContent

public ASN1Sequence readContent()

readReal

public void readReal(ASN1Real t)
              throws ASN1Exception,
                     java.io.IOException
Specified by:
readReal in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readString

public void readString(ASN1String t)
                throws ASN1Exception,
                       java.io.IOException
Specified by:
readString in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readCollection

public void readCollection(ASN1Collection t)
                    throws ASN1Exception,
                           java.io.IOException
Specified by:
readCollection in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readCollectionOf

public void readCollectionOf(ASN1CollectionOf t)
                      throws ASN1Exception,
                             java.io.IOException
Specified by:
readCollectionOf in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readTime

public void readTime(ASN1Time t)
              throws ASN1Exception,
                     java.io.IOException
Specified by:
readTime in interface Decoder
Throws:
ASN1Exception
java.io.IOException

readTaggedType

public void readTaggedType(ASN1TaggedType t)
                    throws ASN1Exception,
                           java.io.IOException
This method also reads in ASN1Opaque types. Opaque types take on the tag and tag class of the decoded type and read in the contents octets into an OCTET STRING. The opaque type can seamlessly be encoded back into the original encoding. No traversal of the inner structure of the encoded type is required.

Specified by:
readTaggedType in interface Decoder
Parameters:
t - The ASN1TaggedType or ASN1Opaque to decode.
Throws:
ASN1Exception
java.io.IOException

readChoice

public void readChoice(ASN1Choice t)
                throws ASN1Exception,
                       java.io.IOException
Reads an ASN.1 CHOICE type. This type is required only on decoding since on encoding the type to encode should be clear. The selection of alternative types is provided by the given ASN1Choice instance. The choice must be unambiguous.

The CHOICE elements must be tagged EXPLICIT. Otherwise, the decoding will abort with an exception. The ASN1Choice class assures this condition upon adding alternative types to the CHOICE type.

Specified by:
readChoice in interface Decoder
Parameters:
t - The collection of choices from which the correct needs to be selected.
Throws:
ASN1Exception
java.io.IOException

readBase128

public int readBase128()
                throws ASN1Exception,
                       java.io.IOException
Reads a base 128 number from this input stream. Each consecutive octet gives 7 bit of the number with all octets but the last one having the most significant bit set to '1'.

This method is used for reading e.g. the long form of ASN.1 tags. However, only tags up to the int range are supported. Note: tags longer than that are not detected, but a wrong number is being returned.

Returns:
The decoded number.
Throws:
ASN1Exception - iff the end of the data is reached before the number is decoded.
java.io.IOException - iff guess what...

readBase256

public int readBase256(int num)
                throws ASN1Exception,
                       java.io.IOException
This method reads in an decodes a number in base 256 format. This method is used primarily for decoding the ASN.1 length octets. Only lengths up to the long range are supported. Even that is probably too much since we'll hardly have to account for computers with more than 264 bytes of memory, right?

The number is expected to be num bytes long. Certain bad encodings are tolerated by this implementation and may lead to errors in rare cases. For instance this implementation does not throw an error if the number of octets of the encoded number is not minimal (e.g. has leading zeroes). Hence, the re-encoding of such a decoded number will result in an encoding that is not identical to the original one.

Parameters:
num - The number of octets of the base 256 number to read from the input stream.
Returns:
The decoded number.
Throws:
ASN1Exception - iff the end of the stream is reached before the number could be decoded completely.
java.io.IOException

read

public int read()
         throws java.io.IOException
This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually read.

Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually read.

Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually read.

Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
This method calls the corresponding method of the underlying stream and increases the position counter by the number of octets actually skipped.

Overrides:
skip in class java.io.FilterInputStream
Throws:
java.io.IOException

mark

public void mark(int readAheadLimit)
This method calls the corresponding method of the underlying stream and also marks this stream's position, which denotes the overall number of octets already read from this stream.

Overrides:
mark in class java.io.FilterInputStream
Parameters:
readAheadLimit - The maximum number of bytes that can be read and still reset to the marked position.

reset

public void reset()
           throws java.io.IOException
Resets the unerlying input stream to the last marked position and also resets this stream's position to the last marked one.

Overrides:
reset in class java.io.FilterInputStream
Throws:
java.io.IOException

markSupported

public boolean markSupported()
Returns true iff the underlying stream supports marking of stream positions. This should better be the case, otherwise an exception is raised when objects or headers are read from this stream.

Overrides:
markSupported in class java.io.FilterInputStream
Returns:
true if marking is supported by the underlying stream.

available

public int available()
              throws java.io.IOException
Returns the number of bytes that can be read from the underlying stream without blocking.

Overrides:
available in class java.io.FilterInputStream
Returns:
The number of bytes available.
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
This method closes the underlying stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterInputStream
Throws:
java.io.IOException