Class DerInputStreamHelper

java.lang.Object
com.microej.util.appverifier.util.DerInputStreamHelper

public class DerInputStreamHelper extends Object
A DER format parsing helper class.
Since:
1.0.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte
    Mask for constructed types.
    static final byte
    Mask for explicit tagging.
    static final byte
    Tag value indicating an ASN.1 "BIT STRING" value.
    static final byte
    Tag value indicating an ASN.1 "BMPString" value.
    static final byte
    Tag value indicating an ASN.1 "BOOLEAN" value.
    static final byte
    Tag value including an ASN.1 "ENUMERATED" value
    static final byte
    Tag value indicating an ASN.1 "GenerallString" value.
    static final byte
    Tag value indicating an ASN.1 "GeneralizedTime" value.
    static final byte
    Tag value including an ASCII string
    static final byte
    Tag value indicating an ASN.1 "INTEGER" value.
    static final byte
    Tag value indicating an ASN.1 "NULL" value.
    static final byte
    Tag value indicating an ASN.1 "OBJECT IDENTIFIER" value.
    static final byte
    Tag value indicating an ASN.1 "OCTET STRING" value.
    static final byte
    Tag value including a "printable" string
    static final byte
    Tag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).
    static final byte
    Tag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).
    static final byte
    Tag value indicating an ASN.1 "SET" (zero to N members, order does not matter).
    static final byte
    Tag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).
    static final byte
    Tag value including a "teletype" string
    static final byte
    Tag value indicating an ASN.1 "UniversalString" value.
    static final byte
    Tag value indicating an ASN.1 "UTCTime" value.
    static final byte
    Tag value indicating an ASN.1 "UTF8String" value.
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    expectTag(byte expectedTag, InputStream stream)
    Reads next byte from the InputStream and verifies if the tag matches the expected tag.
    static byte[]
    Reads the next DER-encoded BitString value from the specified InputStream, and returns it as a byte[].
    static boolean
    Reads the next DER-encoded Boolean value from the specified InputStream, and returns it as a boolean.
    static int
    Reads a DER-encoded value length from an input stream.
    static void
    readFully(InputStream in, byte[] buffer, int off, int len)
    See the general contract of the readFully method of DataInput.
    static int
    Reads the next DER-encoded Integer value from the specified InputStream, and returns it as an integer.
    static void
    Reads the next DER-encoded NULL value from the specified InputStream, as a null value.
    static String
    Reads the next DER-encoded ObjectID value from the specified InputStream, and returns it as a String representation in dotted-decimal notation (for example 1.2.840.113549.1.1.11).
    static String
    Reads the next DER-encoded value from the specified InputStream, and returns it as a String.
    static String
    Reads the next DER-encoded UTF8String value from the specified InputStream, and returns it as a String.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TAG_BOOLEAN

      public static final byte TAG_BOOLEAN
      Tag value indicating an ASN.1 "BOOLEAN" value.
      See Also:
    • TAG_INTEGER

      public static final byte TAG_INTEGER
      Tag value indicating an ASN.1 "INTEGER" value.
      See Also:
    • TAG_BIT_STRING

      public static final byte TAG_BIT_STRING
      Tag value indicating an ASN.1 "BIT STRING" value.
      See Also:
    • TAG_OCTET_STRING

      public static final byte TAG_OCTET_STRING
      Tag value indicating an ASN.1 "OCTET STRING" value.
      See Also:
    • TAG_NULL

      public static final byte TAG_NULL
      Tag value indicating an ASN.1 "NULL" value.
      See Also:
    • TAG_OBJECT_ID

      public static final byte TAG_OBJECT_ID
      Tag value indicating an ASN.1 "OBJECT IDENTIFIER" value.
      See Also:
    • TAG_ENUMERATED

      public static final byte TAG_ENUMERATED
      Tag value including an ASN.1 "ENUMERATED" value
      See Also:
    • TAG_UTF8_STRING

      public static final byte TAG_UTF8_STRING
      Tag value indicating an ASN.1 "UTF8String" value.
      See Also:
    • TAG_PRINTABLE_STRING

      public static final byte TAG_PRINTABLE_STRING
      Tag value including a "printable" string
      See Also:
    • TAG_T61_STRING

      public static final byte TAG_T61_STRING
      Tag value including a "teletype" string
      See Also:
    • TAG_IA5_STRING

      public static final byte TAG_IA5_STRING
      Tag value including an ASCII string
      See Also:
    • TAG_UTC_TIME

      public static final byte TAG_UTC_TIME
      Tag value indicating an ASN.1 "UTCTime" value.
      See Also:
    • TAG_GENERALIZED_TIME

      public static final byte TAG_GENERALIZED_TIME
      Tag value indicating an ASN.1 "GeneralizedTime" value.
      See Also:
    • TAG_GENERAL_STRING

      public static final byte TAG_GENERAL_STRING
      Tag value indicating an ASN.1 "GenerallString" value.
      See Also:
    • TAG_UNIVERSAL_STRING

      public static final byte TAG_UNIVERSAL_STRING
      Tag value indicating an ASN.1 "UniversalString" value.
      See Also:
    • TAG_BMP_STRING

      public static final byte TAG_BMP_STRING
      Tag value indicating an ASN.1 "BMPString" value.
      See Also:
    • TAG_SEQUENCE

      public static final byte TAG_SEQUENCE
      Tag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).
      See Also:
    • TAG_SEQUENCE_OF

      public static final byte TAG_SEQUENCE_OF
      Tag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).
      See Also:
    • TAG_SET

      public static final byte TAG_SET
      Tag value indicating an ASN.1 "SET" (zero to N members, order does not matter).
      See Also:
    • TAG_SET_OF

      public static final byte TAG_SET_OF
      Tag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).
      See Also:
    • MASK_CONTEXT

      public static final byte MASK_CONTEXT
      Mask for explicit tagging.
      See Also:
    • MASK_CONSTRUCTED

      public static final byte MASK_CONSTRUCTED
      Mask for constructed types.
      See Also:
  • Method Details

    • readFully

      public static void readFully(InputStream in, byte[] buffer, int off, int len) throws IOException
      See the general contract of the readFully method of DataInput.

      Bytes for this operation are read from the contained input stream.

      Parameters:
      in - input stream from which to read.
      buffer - the buffer into which the data is read.
      off - the start offset of the data.
      len - the number of bytes to read.
      Throws:
      EOFException - if this input stream reaches the end before reading all the bytes.
      IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
    • readInteger

      public static int readInteger(InputStream is) throws DerFormatException, IOException
      Reads the next DER-encoded Integer value from the specified InputStream, and returns it as an integer.
      Parameters:
      is - the InputStream to parse
      Returns:
      the read value, as an int
      Throws:
      DerFormatException - if the value is not correctly formatted.
      IOException - if an I/O error occurs.
    • readUTF8String

      public static String readUTF8String(InputStream is) throws IOException, DerFormatException
      Reads the next DER-encoded UTF8String value from the specified InputStream, and returns it as a String.
      Parameters:
      is - the InputStream to parse
      Returns:
      the read value, as a String
      Throws:
      DerFormatException - if the value is not correctly formatted.
      IOException - if an I/O error occurs.
    • readString

      public static String readString(InputStream is) throws IOException, DerFormatException
      Reads the next DER-encoded value from the specified InputStream, and returns it as a String. Supported string types are:
      • UTF8String
      • IA5String
      • PrintableString
      Parameters:
      is - the InputStream to parse
      Returns:
      the read value, as a String
      Throws:
      DerFormatException - if the value is not correctly formatted.
      IOException - if an I/O error occurs.
    • readBoolean

      public static boolean readBoolean(InputStream is) throws DerFormatException, IOException
      Reads the next DER-encoded Boolean value from the specified InputStream, and returns it as a boolean.
      Parameters:
      is - the InputStream to parse
      Returns:
      the read value, as a boolean
      Throws:
      DerFormatException - if the value is not correctly formatted.
      IOException - if an I/O error occurs.
    • readOIDString

      public static String readOIDString(InputStream is) throws IOException, DerFormatException
      Reads the next DER-encoded ObjectID value from the specified InputStream, and returns it as a String representation in dotted-decimal notation (for example 1.2.840.113549.1.1.11).

      This method does not check the encoding.

      Parameters:
      is - the InputStream to parse
      Returns:
      the read value, as String representing the OID.
      Throws:
      DerFormatException - if the value is not correctly formatted.
      IOException - if an I/O error occurs.
    • readNull

      public static void readNull(InputStream is) throws IOException, DerFormatException
      Reads the next DER-encoded NULL value from the specified InputStream, as a null value. Obviously returns nothing.
      Parameters:
      is - the InputStream to parse
      Throws:
      DerFormatException - if the value is not correctly formatted.
      IOException - if an I/O error occurs.
    • readBitString

      public static byte[] readBitString(InputStream is) throws IOException, DerFormatException
      Reads the next DER-encoded BitString value from the specified InputStream, and returns it as a byte[].
      Parameters:
      is - the InputStream to parse
      Returns:
      the read value, as a byte[]
      Throws:
      DerFormatException - if the value is not correctly formatted.
      IOException - if an I/O error occurs.
    • expectTag

      public static int expectTag(byte expectedTag, InputStream stream) throws IOException, DerFormatException
      Reads next byte from the InputStream and verifies if the tag matches the expected tag. If so returns length of the value, otherwise throws an IOException
      Parameters:
      expectedTag - tag expected according to the stream format.
      stream - stream to be parsed.
      Returns:
      lengths of the value if the read tag matches the expected tag.
      Throws:
      IOException - if an I/O error occurs.
      DerFormatException - if an error occurs while parsing.
    • readDERLength

      public static int readDERLength(InputStream in) throws IOException, DerFormatException
      Reads a DER-encoded value length from an input stream. The length is encoded as follow
      • Short form: only one byte, between 0 and 127
      • Long form: 1 first byte, whose 8th bit is set to 1, and 7 other bits indicate the following number of bytes storing the length as a big endian number
      Limitation : since this method returns an int, the length should be store in at most 4 bytes
      Parameters:
      in - the InputStream to read from.
      Returns:
      the encoded length, as an integer.
      Throws:
      IOException - if an I/O error occurs.
      DerFormatException - if an error occurs while parsing.