Class DERDecoder

  • All Implemented Interfaces:
    ASN1Decoder

    public class DERDecoder
    extends Object
    implements ASN1Decoder
    A class used to decode ASN.1 values that have been encoded using the Distinguished Encoding Rules (DER).
    Author:
    Farah Juma
    • Constructor Detail

      • DERDecoder

        public DERDecoder​(byte[] buf)
        Create a DER decoder that will decode values from the given byte array.
        Parameters:
        buf - the byte array to decode
      • DERDecoder

        public DERDecoder​(byte[] buf,
                          int offset,
                          int length)
        Create a DER decoder that will decode values from the given byte array.
        Parameters:
        buf - the byte array to decode
        offset - the offset in the byte array of the first byte to read
        length - the maximum number of bytes to read from the byte array
    • Method Detail

      • endSequence

        public void endSequence()
                         throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Advance to the end of a sequence. If there are any elements in the sequence that have not yet been decoded, they will be discarded.
        Specified by:
        endSequence in interface ASN1Decoder
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the sequence
      • endSet

        public void endSet()
                    throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Advance to the end of a set. If there are any elements in the set that have not yet been decoded, they will be discarded.
        Specified by:
        endSet in interface ASN1Decoder
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the set
      • endSetOf

        public void endSetOf()
                      throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Advance to the end of a "set of" element. If there are any elements in the set that have not yet been decoded, they will be discarded.
        Specified by:
        endSetOf in interface ASN1Decoder
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the set
      • startExplicit

        public void startExplicit​(int number)
                           throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Start decoding an ASN.1 explicitly tagged element. All subsequent decode operations will decode elements from this explicitly tagged element until ASN1Decoder.endExplicit() is called.
        Specified by:
        startExplicit in interface ASN1Decoder
        Parameters:
        number - the tag number for the explicit, context-specific tag
        Throws:
        ASN1Exception - if the next element's type does not match the given type
      • startExplicit

        public void startExplicit​(int clazz,
                                  int number)
                           throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Start decoding an ASN.1 explicitly tagged element. All subsequent decode operations will decode elements from this explicitly tagged element until ASN1Decoder.endExplicit() is called.
        Specified by:
        startExplicit in interface ASN1Decoder
        Parameters:
        clazz - the class for the explicit tag
        number - the tag number for the explicit tag
        Throws:
        ASN1Exception - if the next element's type does not match the given type
      • endExplicit

        public void endExplicit()
                         throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Advance to the end of an explicitly tagged element. If there are any elements within the explicitly tagged element that have not yet been decoded, they will be discarded.
        Specified by:
        endExplicit in interface ASN1Decoder
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the explicitly tagged element
      • decodeOctetString

        public byte[] decodeOctetString()
                                 throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as an octet string.
        Specified by:
        decodeOctetString in interface ASN1Decoder
        Returns:
        the decoded octet string, as a byte array
        Throws:
        ASN1Exception - if the next element is not an octet string
      • decodeOctetStringAsString

        public String decodeOctetStringAsString​(String charSet)
                                         throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as an octet string.
        Specified by:
        decodeOctetStringAsString in interface ASN1Decoder
        Parameters:
        charSet - the character set to use when decoding
        Returns:
        the decoded octet string
        Throws:
        ASN1Exception - if the next element is not an octet string
      • decodeIA5StringAsBytes

        public byte[] decodeIA5StringAsBytes()
                                      throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as an IA5 string.
        Specified by:
        decodeIA5StringAsBytes in interface ASN1Decoder
        Returns:
        the decoded IA5 string, as a byte array
        Throws:
        ASN1Exception - if the next element is not an IA5 string
      • decodeBitString

        public byte[] decodeBitString()
                               throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as a bit string.
        Specified by:
        decodeBitString in interface ASN1Decoder
        Returns:
        the decoded bit string as a byte array, with any unused bits removed
        Throws:
        ASN1Exception - if the next element is not a bit string
      • decodeBitStringAsString

        public String decodeBitStringAsString()
                                       throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as a bit string.
        Specified by:
        decodeBitStringAsString in interface ASN1Decoder
        Returns:
        the decoded bit string as a binary string, with any unused bits removed
        Throws:
        ASN1Exception - if the next element is not a bit string
      • decodePrintableStringAsBytes

        public byte[] decodePrintableStringAsBytes()
                                            throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as a PrintableString.
        Specified by:
        decodePrintableStringAsBytes in interface ASN1Decoder
        Returns:
        the decoded PrintableString as a byte array
        Throws:
        ASN1Exception - if the next element is not a PrintableString
      • decodeUniversalStringAsBytes

        public byte[] decodeUniversalStringAsBytes()
                                            throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as an UniversalString.
        Specified by:
        decodeUniversalStringAsBytes in interface ASN1Decoder
        Returns:
        the decoded UniversalString as a byte array
        Throws:
        ASN1Exception - if the next element is not a UniversalString
      • decodeUtf8StringAsBytes

        public byte[] decodeUtf8StringAsBytes()
                                       throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as an UTF8String.
        Specified by:
        decodeUtf8StringAsBytes in interface ASN1Decoder
        Returns:
        the decoded UTF8String as a byte array
        Throws:
        ASN1Exception - if the next element is not a UTF8String
      • decodeBMPStringAsBytes

        public byte[] decodeBMPStringAsBytes()
                                      throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as an BMPString.
        Specified by:
        decodeBMPStringAsBytes in interface ASN1Decoder
        Returns:
        the decoded BMPString as a byte array
        Throws:
        ASN1Exception - if the next element is not a BMPString
      • decodeImplicit

        public void decodeImplicit​(int number)
        Description copied from interface: ASN1Decoder
        Indicate that the next ASN.1 element has the given implicit, context-specific tag.
        Specified by:
        decodeImplicit in interface ASN1Decoder
        Parameters:
        number - the tag number for the implicit tag
      • decodeImplicit

        public void decodeImplicit​(int clazz,
                                   int number)
        Description copied from interface: ASN1Decoder
        Indicate that the next ASN.1 element has the given implicit tag.
        Specified by:
        decodeImplicit in interface ASN1Decoder
        Parameters:
        clazz - the class for the implicit tag
        number - the tag number for the implicit tag
      • decodeBoolean

        public boolean decodeBoolean()
                              throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Decode the next ASN.1 element as a boolean value.
        Specified by:
        decodeBoolean in interface ASN1Decoder
        Returns:
        the decoded boolean value
        Throws:
        ASN1Exception - if the next element is not a boolean value
      • isNextType

        public boolean isNextType​(int clazz,
                                  int number,
                                  boolean isConstructed)
        Description copied from interface: ASN1Decoder
        Determine if the type of the next ASN.1 element matches the given type without actually decoding the next element. This method can be used to determine if an optional ASN.1 value has been included in the encoding or not.
        Specified by:
        isNextType in interface ASN1Decoder
        Parameters:
        clazz - the tag class to match against
        number - the tag number to match against
        isConstructed - whether or not the next element should be constructed
        Returns:
        true if the type of the next ASN.1 element matches the given type and false otherwise
      • peekType

        public int peekType()
                     throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Retrieve the type of the next ASN.1 element without actually decoding the next element.
        Specified by:
        peekType in interface ASN1Decoder
        Returns:
        the type of the next ASN.1 element
        Throws:
        ASN1Exception - if an error occurs while determining the type of the next element
      • hasNextElement

        public boolean hasNextElement()
        Description copied from interface: ASN1Decoder
        Determine if there is at least one more ASN.1 element that can be read. If called while decoding a constructed element (i.e., while decoding a sequence, set, or explicitly tagged element), this method will return whether the constructed element has at least one more ASN.1 element that can be read. Otherwise, this method will return whether at least one more ASN.1 element can be read from the input stream.
        Specified by:
        hasNextElement in interface ASN1Decoder
        Returns:
        true if there is at least one more ASN.1 element that can be read and false otherwise
      • drainElementValue

        public byte[] drainElementValue()
                                 throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Drain the value bytes from the next ASN.1 element.
        Specified by:
        drainElementValue in interface ASN1Decoder
        Returns:
        the value bytes from the next ASN.1 element, as a byte array
        Throws:
        ASN1Exception - if the value bytes from the next ASN.1 element cannot be obtained
      • drainElement

        public byte[] drainElement()
                            throws ASN1Exception
        Description copied from interface: ASN1Decoder
        Drain all of the bytes from the next ASN.1 element.
        Specified by:
        drainElement in interface ASN1Decoder
        Returns:
        all of the bytes from the next ASN.1 element
        Throws:
        ASN1Exception - if the bytes from the next ASN.1 element cannot be obtained
      • drain

        public byte[] drain()
        Description copied from interface: ASN1Decoder
        Drain all of the remaining bytes from the input stream.
        Specified by:
        drain in interface ASN1Decoder
        Returns:
        all of the remaining bytes from the input stream
      • decodeObjectIdentifierAsKeyAlgorithm

        public String decodeObjectIdentifierAsKeyAlgorithm()
        Decodes an OID and resolve its corresponding key algorithm.
        Returns:
        the key algorithm associated with the OID or null if no algorithm could be resolved