Class DERDecoder

  • Direct Known Subclasses:
    X509SubjectPublicKeyInfo

    public class DERDecoder
    extends Object
    Provides the means to navigate through a DER-encoded byte array, to help in decoding the contents.

    It maintains a "current position" in the array that advances with each operation, providing a simple means to handle the type-length-value encoding of DER. For example

       decoder.expect(TYPE);
       int length = decoder.getLength();
       byte[] value = decoder.getBytes(len);
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static byte TYPE_BIT_STRING
      DER type identifier for a bit string value
      static byte TYPE_OCTET_STRING
      DER type identifier for a octet string value
      static byte TYPE_SEQUENCE
      DER type identifier for a sequence value
    • Constructor Summary

      Constructors 
      Constructor Description
      DERDecoder​(byte[] derEncoded)
      Construct a DERDecoder for the given byte array.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void expect​(byte val)
      Confirm that the byte at the current position matches the given value.
      void expect​(int val)
      Confirm that the byte at the current position matches the given value.
      byte[] getBytes​(int length)
      Return an array of bytes from the current position.
      int getLength()
      Get the DER length at the current position.
      void reset()
      Reset the current position to the start of the array.
      void skip​(int length)
      Advance the current position by the given number of bytes.
      boolean test​(byte val)
      Test if the byte at the current position matches the given value.
    • Field Detail

      • TYPE_BIT_STRING

        public static final byte TYPE_BIT_STRING
        DER type identifier for a bit string value
        See Also:
        Constant Field Values
      • TYPE_OCTET_STRING

        public static final byte TYPE_OCTET_STRING
        DER type identifier for a octet string value
        See Also:
        Constant Field Values
      • TYPE_SEQUENCE

        public static final byte TYPE_SEQUENCE
        DER type identifier for a sequence value
        See Also:
        Constant Field Values
    • Constructor Detail

      • DERDecoder

        public DERDecoder​(byte[] derEncoded)
                   throws WSSecurityException
        Construct a DERDecoder for the given byte array.
        Parameters:
        derEncoded - the DER-encoded array to decode.
        Throws:
        WSSecurityException - if the given array is null.
    • Method Detail

      • reset

        public void reset()
        Reset the current position to the start of the array.
      • skip

        public void skip​(int length)
                  throws WSSecurityException
        Advance the current position by the given number of bytes.
        Parameters:
        length - the number of bytes to skip.
        Throws:
        WSSecurityException - if length is negative.
      • expect

        public void expect​(int val)
                    throws WSSecurityException
        Confirm that the byte at the current position matches the given value.
        Parameters:
        val - the expected next byte.
        Throws:
        WSSecurityException - if the current position is at the end of the array, or if the byte at the current position doesn't match the expected value.
      • expect

        public void expect​(byte val)
                    throws WSSecurityException
        Confirm that the byte at the current position matches the given value.
        Parameters:
        val - the expected next byte.
        Throws:
        WSSecurityException - if the current position is at the end of the array, or if the byte at the current position doesn't match the expected value.
      • test

        public boolean test​(byte val)
                     throws WSSecurityException
        Test if the byte at the current position matches the given value.
        Parameters:
        val - the value to test for a match with the current byte.
        Returns:
        true if the byte at the current position matches the given value.
        Throws:
        WSSecurityException - if the current position is at the end of the array.
      • getLength

        public int getLength()
                      throws WSSecurityException
        Get the DER length at the current position.

        DER length is encoded as

        • If the first byte is 0x00 to 0x7F, it describes the actual length.
        • If the first byte is 0x80 + n with 0The length value 0x80, used only in constructed types, is defined as "indefinite length".
        Returns:
        the length, -1 for indefinite length.
        Throws:
        WSSecurityException - if the current position is at the end of the array or there is an incomplete length specification.
      • getBytes

        public byte[] getBytes​(int length)
                        throws WSSecurityException
        Return an array of bytes from the current position.
        Parameters:
        length - the number of bytes to return.
        Returns:
        an array of the requested number of bytes from the current position.
        Throws:
        WSSecurityException - if the current position is at the end of the array, or the length is negative.