org.jasn
Class BerDecoder

java.lang.Object
  extended byorg.jasn.BerDecoder
All Implemented Interfaces:
Asn1Decoder
Direct Known Subclasses:
DerDecoder

public class BerDecoder
extends Object
implements Asn1Decoder

A decoder for parsing BER encoded ASN.1 values from an input stream.

Author:
Nicolas Vraux
To do:
implements decodeUTF8 with its own decoding method, implements decodeGeneralizedTime

Constructor Summary
BerDecoder(InputStream in)
          Creates a decoder for parsing BER encoded ASN.1 values from the specified input stream.
BerDecoder(InputStream in, int bufSize)
          Creates a decoder for parsing BER encoded ASN.1 values from the specified input stream.
 
Method Summary
 byte[] decodeAny()
           
 void decodeAny(OutputStream out)
          Decodes an ASN.1 ANY value from the underlying stream and copies it to the specified output stream.
 BitString decodeBitString()
          Decodes an ASN.1 BIT STRING value from the underlying stream and returns it as a BitString value.
 BitString decodeBitString(BitString set)
          Decodes an ASN.1 BIT STRING value from the underlying stream and appends its bit values to the specified BitString.
 String decodeBMPString()
          Decodes an ASN.1 BMPString value from the underlying input stream and return it as a String object.
 StringBuffer decodeBMPString(StringBuffer buf)
          Decodes an ASN.1 BMPString value from the underlying stream and appends its string representation to the specified StringBuffer.
 boolean decodeBoolean()
          Decodes an ASN.1 BOOLEAN value from the underlying stream and returns it as a boolean value.
 int decodeChoice()
          Starts the decoding of a BER encoded CHOICE value.
 int decodeConstructed(int tag)
          Starts the decoding of a BER encoded ASN.1 constructed value.
 void decodeEndOf(int id)
          Terminates the decoding of the constructed value identified by id is finished.
 BigInteger decodeEnumerated()
          Decodes an ASN.1 ENUMERATED value from the underlying stream and returns it as a BigInteger value.
 int decodeEnumeratedAsInt()
          Decodes an ASN.1 ENUMERATED value from the underlying stream and returns it as a int value.
 long decodeEnumeratedAsLong()
          Decodes an ASN.1 ENUMERATED value from the underlying stream and returns it as a int value.
 int decodeExplicit(int tag)
          Indicates that the next decoded ASN.1 value has an explicit tag set equals to the specified tag value.
 Calendar decodeGeneralizedTime()
          Decodes an ASN.1 GeneralizedTime value from the underlying input stream and return it as a Calendar object.
 String decodeIA5String()
          Decodes an ASN.1 IA5String value from the underlying input stream and return it as a String object.
 StringBuffer decodeIA5String(StringBuffer buf)
          Decodes an ASN.1 IA5String value from the underlying input stream and appends it to the specified StringBuffer.
 void decodeImplicit(int tag)
          Indicates that the next value to be decoded has the specified implicit tag.
 BigInteger decodeInteger()
          Decodes an ASN.1 INTEGER value from the underlying stream and returns it as a BigInteger value.
 int decodeIntegerAsInt()
          Decodes an ASN.1 INTEGER value from the underlying stream and returns it as a int value.
 long decodeIntegerAsLong()
          Decodes an ASN.1 INTEGER value from the underlying stream and returns it as a long value.
 void decodeNull()
          Decodes the next ASN.1 NULL value from the underlying stream.
 String decodeNumericString()
          Decodes an ASN.1 NumericString value from the underlying input stream and return it as a String object.
 StringBuffer decodeNumericString(StringBuffer buf)
          Decodes an ASN.1 NumericString value from the underlying input stream and appends it to the specified StringBuffer.
 ObjectIdentifier decodeObjectIdentifier()
          Decodes a BER encoded OBJECT IDENTIFIER.
 byte[] decodeOctetString()
          Decodes a BER encoded ASN.1 OCTET STRING value and returns it as a byte array.
 void decodeOctetString(OutputStream buf)
          Decodes a BER encoded ASN.1 OCTET STRING value and copies its contents into the specified output stream.
 String decodePrintableString()
          Decodes an ASN.1 PrintableString value from the underlying input stream and return it as a String object.
 StringBuffer decodePrintableString(StringBuffer buf)
          Decodes an ASN.1 PrintableString value from the underlying input stream and appends it to the specified StringBuffer.
 int decodeSequence()
          Starts the decoding of a BER encoded SEQUENCE.
 int decodeSequenceOf()
          Starts the decoding of a BER encoded SEQUENCE OF.
 int decodeSet()
          Starts the decoding of a BER encoded SET.
 int decodeSetOf()
          Starts the decoding of a BER encoded SET OF.
 void decodeString(int tag, OutputStream out)
          Decodes the next ASN.1 character string value and appends its contents bytes to the specified output stream.
 String decodeTeletexString()
          Decodes an ASN.1 TeletexString value from the underlying input stream and return it as a String object.
 StringBuffer decodeTeletexString(StringBuffer buf)
          Decodes an ASN.1 TeletexString value from the underlying input stream and appends it to the specified StringBuffer.
 Calendar decodeUTCTime()
          Decodes an ASN.1 UTCTime value from the underlying input stream and return it as a Date object.
 String decodeUTF8String()
          Decodes an ASN.1 UTF8String value from the underlying input stream and return it as a String object.
 StringBuffer decodeUTF8String(StringBuffer buf)
          Decodes an ASN.1 UTF8String value from the underlying input stream and appends it to the specified StringBuffer.
 String decodeVisibleString()
          Decodes an ASN.1 VisibleString value from the underlying input stream and return it as a String object.
 StringBuffer decodeVisibleString(StringBuffer buf)
          Decodes an ASN.1 VisibleString value from the underlying input stream and appends it to the specified StringBuffer.
 boolean endOf(int id)
          Indicates wether the decoding of the constructed value identified by id is finished.
 boolean isNextConstructed()
          Indicates wether the next ASN.1 value has a constructed encoding.
 boolean isNextTag(int tag)
          Checks if the tag of the next decoded value is equals to the specified tag value.
protected  void matchTag(int tag, boolean constructed)
          Decodes a BER encoded tag and checks that its value matches the specified value and that is is constructed if constructed is true.
 void setInputStream(InputStream in)
          Sets the underlying input stream from which the ASN.1 values are decoded.
 void skipNext()
          Skips the next encoded ASN.1 value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BerDecoder

public BerDecoder(InputStream in,
                  int bufSize)
Creates a decoder for parsing BER encoded ASN.1 values from the specified input stream.

Parameters:
in - the underlying input stream.
bufSize - the size of the internal buffer used for decoding.
Throws:
NullPointerException - if the given input stream is null.

BerDecoder

public BerDecoder(InputStream in)
Creates a decoder for parsing BER encoded ASN.1 values from the specified input stream. A default buffer size of 1024 is used.

Parameters:
in - the underlying input stream.
Method Detail

setInputStream

public void setInputStream(InputStream in)
Sets the underlying input stream from which the ASN.1 values are decoded. This makes it possible to parse different source without creating a new decoder for each one.

Parameters:
in - the new input stream to parse from.

decodeAny

public void decodeAny(OutputStream out)
               throws IOException
Decodes an ASN.1 ANY value from the underlying stream and copies it to the specified output stream.

Note: The complete ASN.1 encoding (including tag, length and content's bytes) are copied into the stream.

Specified by:
decodeAny in interface Asn1Decoder
Parameters:
out - the output stream the decoded ANY value are copied into.
Throws:
NullPointerException - if the given output stream is null.
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded value.

decodeAny

public byte[] decodeAny()
                 throws IOException
Specified by:
decodeAny in interface Asn1Decoder
Throws:
IOException

decodeBoolean

public boolean decodeBoolean()
                      throws IOException
Decodes an ASN.1 BOOLEAN value from the underlying stream and returns it as a boolean value.

Specified by:
decodeBoolean in interface Asn1Decoder
Returns:
the decoded boolean value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded BOOLEAN value.

decodeInteger

public BigInteger decodeInteger()
                         throws IOException
Decodes an ASN.1 INTEGER value from the underlying stream and returns it as a BigInteger value.

The less expensive decodeIntegerAsInt and decodeIntegerAsLong methods should be preferred if there is some restriction on the range of the decoded ASN.1 INTEGER value.

Specified by:
decodeInteger in interface Asn1Decoder
Returns:
a BigInteger representing the content of decoded ASN.1 INTEGER value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded INTEGER value.
See Also:
decodeIntegerAsInt(), decodeIntegerAsLong()

decodeIntegerAsInt

public int decodeIntegerAsInt()
                       throws IOException
Decodes an ASN.1 INTEGER value from the underlying stream and returns it as a int value.

The caller must be sure that the decoded INTEGER can be represented in a 4-bytes int, otherwise a DecodingException is thrown to indicate that an overflow occured.

Specified by:
decodeIntegerAsInt in interface Asn1Decoder
Returns:
a int value representing the content of decoded ASN.1 INTEGER value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded INTEGER value or if the INTEGER value does not fit in a 4-bytes int.

decodeIntegerAsLong

public long decodeIntegerAsLong()
                         throws IOException
Decodes an ASN.1 INTEGER value from the underlying stream and returns it as a long value.

The caller must be sure that the decoded INTEGER can be represented in a 8-bytes long, otherwise a DecodingException is thrown to indicate that an overflow occured.

Note:This method is currently not implemented and always throws a UnsupportedOperationException when it is invoked.

Specified by:
decodeIntegerAsLong in interface Asn1Decoder
Returns:
a long value representing the content of decoded ASN.1 INTEGER value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded INTEGER value or if the INTEGER value does not fit in a 8-bytes long.

decodeBitString

public BitString decodeBitString()
                          throws IOException
Decodes an ASN.1 BIT STRING value from the underlying stream and returns it as a BitString value. The first bit value of the returned BitString is equals to the first bit value of the decoded BIT STRING value, and so on.

The total number of bit contained in the decoded BIT STRING may be retrieved by invoking the length method of the returned BitString.

This method is equivalent to decodeBitString(new BitString());

Specified by:
decodeBitString in interface Asn1Decoder
Returns:
a BitString instance representing the content of the decoded BIT STRING.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded BIT STRING value.
See Also:
decodeBitString(BitString)

decodeBitString

public BitString decodeBitString(BitString set)
                          throws IOException
Decodes an ASN.1 BIT STRING value from the underlying stream and appends its bit values to the specified BitString. The first bit of the decode BIT STRING value corresponds to the bit at index set.length() + 1 of the BitString.

The total number of bit contained in the decoded BIT STRING may be retrieved by invoking the length method of the returned BitString.

Specified by:
decodeBitString in interface Asn1Decoder
Parameters:
set - the BitString to which the decoded bits are appended.
Returns:
a BitString instance representing the content of the decoded BIT STRING.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded BIT STRING value.

decodeOctetString

public byte[] decodeOctetString()
                         throws IOException
Decodes a BER encoded ASN.1 OCTET STRING value and returns it as a byte array.

Specified by:
decodeOctetString in interface Asn1Decoder
Returns:
an array of bytes representing the content of the decoded OCTET STRING. The first byte of the returned array corresponds to the first byte of the OCTET STRING, etc.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded OCTET STRING value.

decodeOctetString

public void decodeOctetString(OutputStream buf)
                       throws IOException
Decodes a BER encoded ASN.1 OCTET STRING value and copies its contents into the specified output stream.

Specified by:
decodeOctetString in interface Asn1Decoder
Parameters:
buf - the output stream receiving the decoded octets.
Throws:
NullPointerException - if the given output stream is null.
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded OCTET STRING value.

decodeNull

public void decodeNull()
                throws IOException
Decodes the next ASN.1 NULL value from the underlying stream.

Specified by:
decodeNull in interface Asn1Decoder
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded NULL value.

decodeEnumerated

public BigInteger decodeEnumerated()
                            throws IOException
Decodes an ASN.1 ENUMERATED value from the underlying stream and returns it as a BigInteger value.

The less expensive decodeEnumeratedAsInt and decodeEnumeratedAsLong methods should be preferred if there is some restriction on the range of the decoded ASN.1 ENUMERATED value, which is the common case if no extension has been used in the type specification.

Specified by:
decodeEnumerated in interface Asn1Decoder
Returns:
a BigInteger representing the content of decoded ASN.1 ENUMERATED value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded INTEGER value.
See Also:
decodeEnumeratedAsInt(), decodeEnumeratedAsLong()

decodeEnumeratedAsInt

public int decodeEnumeratedAsInt()
                          throws IOException
Decodes an ASN.1 ENUMERATED value from the underlying stream and returns it as a int value.

The caller must be sure that the decoded ENUMERATED can be represented in a 4-bytes int, otherwise a DecodingException is thrown to indicate that an overflow occured.

Specified by:
decodeEnumeratedAsInt in interface Asn1Decoder
Returns:
a int value representing the content of decoded ASN.1 ENUMERATED value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded ENUMERATED value or if the ENUMERATED value does not fit in a 4-bytes int.

decodeEnumeratedAsLong

public long decodeEnumeratedAsLong()
                            throws IOException
Decodes an ASN.1 ENUMERATED value from the underlying stream and returns it as a int value.

The caller must be sure that the decoded ENUMERATED can be represented in a 8-bytes int, otherwise a DecodingException is thrown to indicate that an overflow occured.

Specified by:
decodeEnumeratedAsLong in interface Asn1Decoder
Returns:
a int value representing the content of decoded ASN.1 ENUMERATED value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded ENUMERATED value or if the ENUMERATED value does not fit in a 8-bytes int.

decodeObjectIdentifier

public ObjectIdentifier decodeObjectIdentifier()
                                        throws IOException
Decodes a BER encoded OBJECT IDENTIFIER.

Specified by:
decodeObjectIdentifier in interface Asn1Decoder
Returns:
the decoded ObjectIdentifier.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded ObjectIdentifier value.

decodeUTF8String

public String decodeUTF8String()
                        throws IOException
Decodes an ASN.1 UTF8String value from the underlying input stream and return it as a String object.

Specified by:
decodeUTF8String in interface Asn1Decoder
Returns:
the decoded string.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded UTF8String value.

decodeUTF8String

public StringBuffer decodeUTF8String(StringBuffer buf)
                              throws IOException
Decodes an ASN.1 UTF8String value from the underlying input stream and appends it to the specified StringBuffer.

Specified by:
decodeUTF8String in interface Asn1Decoder
Parameters:
buf - the buffer the decoded string is appended to.
Returns:
the passed StringBuffer the decoded UTF8String is appended to.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded UTF8String value.

decodeNumericString

public String decodeNumericString()
                           throws IOException
Decodes an ASN.1 NumericString value from the underlying input stream and return it as a String object.

Specified by:
decodeNumericString in interface Asn1Decoder
Returns:
the decoded string.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded NumericString value.

decodeNumericString

public StringBuffer decodeNumericString(StringBuffer buf)
                                 throws IOException
Decodes an ASN.1 NumericString value from the underlying input stream and appends it to the specified StringBuffer.

Specified by:
decodeNumericString in interface Asn1Decoder
Parameters:
buf - the buffer the decoded string is appended to.
Returns:
the passed StringBuffer the decoded UTF8String is appended to.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded NumericString value.

decodeIA5String

public String decodeIA5String()
                       throws IOException
Decodes an ASN.1 IA5String value from the underlying input stream and return it as a String object.

Specified by:
decodeIA5String in interface Asn1Decoder
Returns:
the decoded string.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded IA5String value.

decodeIA5String

public StringBuffer decodeIA5String(StringBuffer buf)
                             throws IOException
Decodes an ASN.1 IA5String value from the underlying input stream and appends it to the specified StringBuffer.

Specified by:
decodeIA5String in interface Asn1Decoder
Parameters:
buf - the buffer the decoded string is appended to.
Returns:
the passed StringBuffer the decoded UTF8String is appended to.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded IA5String value.

decodePrintableString

public String decodePrintableString()
                             throws IOException
Decodes an ASN.1 PrintableString value from the underlying input stream and return it as a String object.

Specified by:
decodePrintableString in interface Asn1Decoder
Returns:
the decoded string.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded PrintableString value.

decodePrintableString

public StringBuffer decodePrintableString(StringBuffer buf)
                                   throws IOException
Decodes an ASN.1 PrintableString value from the underlying input stream and appends it to the specified StringBuffer.

Specified by:
decodePrintableString in interface Asn1Decoder
Parameters:
buf - the buffer the decoded string is appended to.
Returns:
the passed StringBuffer the decoded PrintableString is appended to.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded PrintableString value.

decodeTeletexString

public String decodeTeletexString()
                           throws IOException
Decodes an ASN.1 TeletexString value from the underlying input stream and return it as a String object.

Specified by:
decodeTeletexString in interface Asn1Decoder
Returns:
the decoded string.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded TeletexString value.

decodeTeletexString

public StringBuffer decodeTeletexString(StringBuffer buf)
                                 throws IOException
Decodes an ASN.1 TeletexString value from the underlying input stream and appends it to the specified StringBuffer.

Specified by:
decodeTeletexString in interface Asn1Decoder
Parameters:
buf - the buffer the decoded string is appended to.
Returns:
the passed StringBuffer the decoded TeletexString is appended to.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded TeletexString value.

decodeVisibleString

public String decodeVisibleString()
                           throws IOException
Decodes an ASN.1 VisibleString value from the underlying input stream and return it as a String object.

Specified by:
decodeVisibleString in interface Asn1Decoder
Returns:
the decoded string.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded VisibleString value.

decodeVisibleString

public StringBuffer decodeVisibleString(StringBuffer buf)
                                 throws IOException
Decodes an ASN.1 VisibleString value from the underlying input stream and appends it to the specified StringBuffer.

Specified by:
decodeVisibleString in interface Asn1Decoder
Parameters:
buf - the buffer the decoded string is appended to.
Returns:
the passed StringBuffer the decoded UTF8String is appended to.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded VisibleString value.

decodeBMPString

public String decodeBMPString()
                       throws IOException
Decodes an ASN.1 BMPString value from the underlying input stream and return it as a String object.

Specified by:
decodeBMPString in interface Asn1Decoder
Returns:
the decoded string.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded VisibleString value.

decodeBMPString

public StringBuffer decodeBMPString(StringBuffer buf)
                             throws IOException
Decodes an ASN.1 BMPString value from the underlying stream and appends its string representation to the specified StringBuffer. The appended StringBuffer is then returned.

Specified by:
decodeBMPString in interface Asn1Decoder
Parameters:
buf - the StringBuffer the decoded string is appended to.
Returns:
the passed StringBuffer the decoded BMPString is appended to.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded BMPString value.

decodeString

public void decodeString(int tag,
                         OutputStream out)
                  throws IOException
Decodes the next ASN.1 character string value and appends its contents bytes to the specified output stream. The decoded string value must have been tagged with the specified tag value.

Specified by:
decodeString in interface Asn1Decoder
Parameters:
tag - the expected tag of the decoded ASN.1 string value.
out - the output stream to which the contents bytes are to be appended.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded character string value.

decodeUTCTime

public Calendar decodeUTCTime()
                       throws IOException
Decodes an ASN.1 UTCTime value from the underlying input stream and return it as a Date object.

Specified by:
decodeUTCTime in interface Asn1Decoder
Returns:
the Date instance representing the decoded UTCTime.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded UTCTime value.

decodeGeneralizedTime

public Calendar decodeGeneralizedTime()
                               throws IOException
Decodes an ASN.1 GeneralizedTime value from the underlying input stream and return it as a Calendar object.

Specified by:
decodeGeneralizedTime in interface Asn1Decoder
Returns:
the Calendar instance representing the decoded GeneralizedTime.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded GeneralizedTime value.

decodeChoice

public int decodeChoice()
                 throws IOException
Starts the decoding of a BER encoded CHOICE value. The tag value of the selected ASN.1 value is returned. It is up to the caller to decode the selected value with the corresponding decoding method.

For example, the value of the CHOICE type

 Type ::= CHOICE { INTEGER i, VisibleString s}
 
must be decoded as follows
 switch (dec.decodeChoice()) {
     case Tags.INTEGER:
         i = dec.decodeInteger();
         break;
     case Tags.VISIBLESTRING:
         s = decodeVisibleString();
 }
 

Specified by:
decodeChoice in interface Asn1Decoder
Returns:
the tag value of the selected ASN.1 value.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the decoder could not recognize a choice value.

decodeSequence

public int decodeSequence()
                   throws IOException
Starts the decoding of a BER encoded SEQUENCE.

An ASN.1 SEQUENCE is an ordered collection of various data element. The decoding of a SEQUENCE must always start with this method; then the elements defined in the SEQUENCE should be decoded with their corresponding decode methods. Finally, the endOf method must be invoked to terminate the decoding of the SEQUENCE.

The returned int value must be passed to the endOf method that then checks if the current nesting and the passed int do match.

The code samples below show the decoding of two nested sequences. The corresponding ASN.1 definition is:

 SEQUENCE {
    s IA5String,
    a SEQUENCE {
        i INTEGER
    }
 }
 
 int id1 = dec.decodeSequence();
     String s = dec.decodeIA5String();
     int id2 = dec.decodeSequence();
         BigInteger i = dec.decodeInteger();
     dec.endOf(id2);
 dec.endOf(id1);
 

Specified by:
decodeSequence in interface Asn1Decoder
Returns:
a scope identifier that must be passed to the endOf method to terminate the decoding of the SEQUENCE.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded SEQUENCE.

decodeSequenceOf

public int decodeSequenceOf()
                     throws IOException
Starts the decoding of a BER encoded SEQUENCE OF.

An ASN.1 SEQUENCE OF is an ordered collection of data elements of the same type. The decoding of a SEQUENCE OF must always start with this method; then the elements defined in the SEQUENCE OF should be decoded with their corresponding decode methods. Finally, the endOf method must be invoked to terminate the decoding of the SEQUENCE OF.

The returned int value must be passed to the endOf method that then checks if the current nesting and the passed int do match.

The code samples below show the decoding of a SEQUENCE OF type. The corresponding ASN.1 definition is:

 MyList ::= SEQUENCE OF PrintableString
 
and the decoding looks like:
 List seq = ...
 int id = dec.decodeSequenceOf();
 while (!dec.endOf(id)) {
     String s = dec.decodePrintableString();
     // do something with the elements
     seq.add(s);
 }
 

Specified by:
decodeSequenceOf in interface Asn1Decoder
Returns:
a sequence identifier that must be passed to the endOf method to terminate the decoding of the SEQUENCE OF.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded SEQUENCE OF.

decodeSet

public int decodeSet()
              throws IOException
Starts the decoding of a BER encoded SET.

An ASN.1 SET is an unordered collection of various data elements. The decoding of a SET must always start with this method; then the elements defined in the SET should be decoded with their corresponding decode methods. Finally, the endOf method must be invoked to terminate the decoding of the SET.

The returned int value must be passed to the endOf method that then checks if the current nesting and the passed int do match.

The code samples below show the decoding of a SET type. The corresponding ASN.1 definition is:

 MySet ::= SET {
    s [0] IA5String,
    a [1] BOOLEAN
 
and the decoding looks like:
 int id1 = dec.decodeSetOf();
 while (!dec.endOf(id1)) {
     if (dec.isNextTag(Tags.createTag(Tags.CONTEXT, 0))) {
        int ctx = dec.decodeExplicit(Tags.createTag(Tags.CONTEXT, 0));
            String s = dec.decodeIA5String();
        dec.decodeEndOf(ctx);
     } else if (dec.isNextTag(Tags.createTag(Tags.CONTEXT, 1))) {
        int ctx = dec.decodeExplicit(Tags.createTag(Tags.CONTEXT, 1));
            boolean b = dec.decodeBoolean();
        dec.decodeEndOf(ctx);
     } else {
        // throw an exception
     }
 }
 

Specified by:
decodeSet in interface Asn1Decoder
Returns:
a sequence identifier that must be passed to the endOf method to terminate the decoding of the SET OF.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded SET.

decodeSetOf

public int decodeSetOf()
                throws IOException
Starts the decoding of a BER encoded SET OF.

An ASN.1 SET OF is an unordered collection of data elements of the same type. The decoding of a SET OF must always start with this method; then the elements defined in the SET OF should be decoded with their corresponding decode methods. Finally, the endOf method must be invoked to terminate the decoding of the SET OF.

The returned int value must be passed to the endOf method that then checks if the current nesting and the passed int do match.

The code samples below show the decoding of a SET OF type. The corresponding ASN.1 definition is:

 MySet ::= SET OF PrintableString
 
and the decoding looks like:
 Set set = ...
 int id = dec.decodeSetOf();
 while (!dec.endOf(id)) {
     String s = dec.decodePrintableString();
     // do something with the elements
     set.add(s);
 }
 

Specified by:
decodeSetOf in interface Asn1Decoder
Returns:
a sequence identifier that must be passed to the endOf method to terminate the decoding of the SET OF.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded SET OF.

endOf

public boolean endOf(int id)
              throws IOException
Indicates wether the decoding of the constructed value identified by id is finished. The decoding of a constructed value is initiated by invoking one of the following methods:

Specified by:
endOf in interface Asn1Decoder
Parameters:
id - the reference to a constructed value. It must be the value returned by the method that initiated the decoding of the referenced constructed value.
Returns:
true if the decoding of the the constructed value referenced by is finished, false otherwise.
Throws:
DecodingException - if the id doesn't reference any constructed value, or if the referenced constructed value has been already decoded.
IOException - if an I/O error occurs.

decodeEndOf

public void decodeEndOf(int id)
                 throws IOException
Terminates the decoding of the constructed value identified by id is finished. The decoder checks that the end of the referenced constructed value has been reached and throws a DecodingException if the constructed value contains some more embedded values.

Specified by:
decodeEndOf in interface Asn1Decoder
Parameters:
id - the reference to a constructed value.
Throws:
IOException - if an I/O error occurs.

skipNext

public void skipNext()
              throws IOException
Skips the next encoded ASN.1 value.

Specified by:
skipNext in interface Asn1Decoder
Throws:
IOException - if an I/O error occurs.

decodeExplicit

public int decodeExplicit(int tag)
                   throws IOException
Indicates that the next decoded ASN.1 value has an explicit tag set equals to the specified tag value.

Explicit tags create a separate (tag, length, value) tripplet where the tagged element is encoded as value.
This method must be called before the ASN.1 value is decoded; then the value itself is handled as usual. Finally, the endOf method must be invoked to terminate the explicitly decoding.

The decoding of the following ASN.1 type

 version [0] EXPLICIT Version
 
looks like:
 int id = dec.decodeExplicit(Tags.makeTag(Tags.CONTEXT, 0));
 BigInteger version = dec.decodeInteger();
 dec.endOf(id);
 

Specified by:
decodeExplicit in interface Asn1Decoder
Parameters:
tag - the explicit tag of the next decoded ASN.1 value. The tag must be a valid tag such as one that has been created with the Tags.createTag(int, int) method.
Returns:
a sequence identifier that must be passed to the endOf method to terminate the decoding of the explicit tagging.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the bytes read do not represent a valid BER encoded ObjectIdentifier value.
See Also:
endOf(int), Tags.createTag(int, int)

decodeImplicit

public void decodeImplicit(int tag)
                    throws IOException
Indicates that the next value to be decoded has the specified implicit tag.

This method may be invoked many time with different implicit tags before invoking the subsequent decoding method, but only the first decodeImplicit method has an effect and all the other tags will be ignored. In other words, the following statements

 dec.decodeImplicit(Tags.makeTag(Tags.CONTEXT, 5));
 ...
 dec.decodeImplicit(Tags.makeTag(Tags.CONTEXT, 7));
 BigInteger i = dec.decodeInteger();
 
are equivalent to
 dec.decodeImplicit(Tags.makeTag(Tags.CONTEXT, 5));
 BigInteger i = dec.decodeInteger();
 
This tag reduction mechanism is useful for decoding ASN.1 type like
 Type1 ::= [APPLICATION 2] IMPLICIT INTEGER
 Type2 ::= [APPLICATION 5] IMPLICIT Type1
 
where the code used to decode value of type Type2 looks like
 dec.decodeImplicit(Tags.makeTag(Tags.APPLICATION, 5));
 dec.decodeImplicit(Tags.makeTag(Tags.APPLICATION, 2));
 dec.decodeInteger();
 

Specified by:
decodeImplicit in interface Asn1Decoder
Parameters:
tag - the implicit tag that is expected for the next decoded value.
Throws:
IOException - if an I/O error occurs.

isNextTag

public boolean isNextTag(int tag)
                  throws IOException
Checks if the tag of the next decoded value is equals to the specified tag value. It is commonly used to verify if an optional value has been encoded or not. For example, a value for the ASN.1 type:
 SEQUENCE {
       a [0] IMPLICIT INTEGER OPTIONAL,
       b BOOLEAN
 }
 
may be decoded as follows:
 int id = dec.decodeSequence();
 if (dec.isNextTag(Tags.createTag(Tags.CONTEXT, 0))) {
       dec.decodeImplicit(Tags.createTag(Tags.CONTEXT, 0));
       a = dec.decodeInteger();
 }

 b = dec.decodeBoolean();
 dec.decodeEndOf(id);
 

Specified by:
isNextTag in interface Asn1Decoder
Parameters:
tag - the expected tag value to check with.
Returns:
true if the next encoded value has the specified tag, false otherwise.
Throws:
IOException - if an I/O error occurs.

isNextConstructed

public boolean isNextConstructed()
                          throws IOException
Indicates wether the next ASN.1 value has a constructed encoding.

Specified by:
isNextConstructed in interface Asn1Decoder
Returns:
true if the encoding is constructed, false otherwise.
Throws:
IOException - if an I/O error occured.

decodeConstructed

public int decodeConstructed(int tag)
                      throws IOException
Starts the decoding of a BER encoded ASN.1 constructed value.

This method decodes a tag value from the stream and verifies that it is constructed and that it's value is equals to tag.

If the encodeImplicit() method has been previously invoked, its passed tag parameter is used instead of the specified tag.

This method creates and returns a unique identifier for referencing the decoded constructed value.

 if (dec.isNextConstructed()) {
    int id = dec.decodeConstructed(Tags.PRINTABLESTRING);
    while (!dec.endOf(id)) {
       System.out.println(dec.decodePrintableString());
    }
  } else {
     System.out.println(dec.decodePrintableString());
  }
 

Specified by:
decodeConstructed in interface Asn1Decoder
Parameters:
tag - the tag of the constructed value to be decoded.
Returns:
an identifier referencing the decoded constructed value. It must be passed to the endOf method to terminate the decoding.
Throws:
IOException - if an I/O error occurs.
DecodingException - if the decoded tag value is not equals to the specified one (or not equals to an implicitely tag value passed by a previous call to the encodeImplicit() method) or if the value has not a constructed encoding.

matchTag

protected void matchTag(int tag,
                        boolean constructed)
                 throws IOException
Decodes a BER encoded tag and checks that its value matches the specified value and that is is constructed if constructed is true.

Parameters:
tag - the tag value of the decoded tag. It must be a value created with the Tag.makeTag() method.
constructed - indicates wether the decoded tag must be constructed or not.
Throws:
DecodingException - if the decoded tag has a value that differs from the specified one or if its constructed flag does not match the specified one.
IOException - if an I/O error occurs.


Copyright © 2004 Nicolas Vraux. All Rights Reserved.