Interface ASN1Decoder

  • All Known Implementing Classes:
    DERDecoder

    public interface ASN1Decoder
    An interface for decoding ASN.1 encoded values from an input stream.
    Author:
    Farah Juma
    • Method Detail

      • startSequence

        void startSequence()
                    throws ASN1Exception
        Start decoding an ASN.1 sequence. All subsequent decode operations will decode elements from this sequence until endSequence() is called.
        Throws:
        ASN1Exception - if the next element is not a sequence
      • endSequence

        void endSequence()
                  throws ASN1Exception
        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.
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the sequence
      • startSet

        void startSet()
               throws ASN1Exception
        Start decoding an ASN.1 set. All subsequent decode operations will decode elements from this set until endSet() is called.
        Throws:
        ASN1Exception - if the next element is not a set
      • endSet

        void endSet()
             throws ASN1Exception
        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.
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the set
      • startSetOf

        void startSetOf()
                 throws ASN1Exception
        Start decoding an ASN.1 "set of" element. All subsequent decode operations will decode elements from this set until endSetOf() is called.
        Throws:
        ASN1Exception - if the next element is not a set
      • endSetOf

        void endSetOf()
               throws ASN1Exception
        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.
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the set
      • startExplicit

        void startExplicit​(int number)
                    throws ASN1Exception
        Start decoding an ASN.1 explicitly tagged element. All subsequent decode operations will decode elements from this explicitly tagged element until endExplicit() is called.
        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

        void startExplicit​(int clazz,
                           int number)
                    throws ASN1Exception
        Start decoding an ASN.1 explicitly tagged element. All subsequent decode operations will decode elements from this explicitly tagged element until endExplicit() is called.
        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

        void endExplicit()
                  throws ASN1Exception
        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.
        Throws:
        ASN1Exception - if an error occurs while advancing to the end of the explicitly tagged element
      • decodeOctetString

        byte[] decodeOctetString()
                          throws ASN1Exception
        Decode the next ASN.1 element as an octet string.
        Returns:
        the decoded octet string, as a byte array
        Throws:
        ASN1Exception - if the next element is not an octet string
      • decodeOctetStringAsString

        String decodeOctetStringAsString()
                                  throws ASN1Exception
        Decode the next ASN.1 element as an octet string.
        Returns:
        the decoded octet string, as a UTF-8 string
        Throws:
        ASN1Exception - if the next element is not an octet string
      • decodeOctetStringAsString

        String decodeOctetStringAsString​(String charSet)
                                  throws ASN1Exception
        Decode the next ASN.1 element as an octet string.
        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
      • decodeIA5String

        String decodeIA5String()
                        throws ASN1Exception
        Decode the next ASN.1 element as an IA5 string.
        Returns:
        the decoded IA5 string
        Throws:
        ASN1Exception - if the next element is not an IA5 string
      • decodeIA5StringAsBytes

        byte[] decodeIA5StringAsBytes()
                               throws ASN1Exception
        Decode the next ASN.1 element as an IA5 string.
        Returns:
        the decoded IA5 string, as a byte array
        Throws:
        ASN1Exception - if the next element is not an IA5 string
      • decodeBitString

        byte[] decodeBitString()
                        throws ASN1Exception
        Decode the next ASN.1 element as a bit string.
        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
      • decodeBitStringAsInteger

        BigInteger decodeBitStringAsInteger()
        Decode the next ASN.1 element as a bit string where the value is a ASN.1 INTEGER.
        Returns:
        a BigInteger decoded from the bit string
        Throws:
        ASN1Exception - if the next element is not a bit string or its value is not an integer
      • decodeBitStringAsString

        String decodeBitStringAsString()
                                throws ASN1Exception
        Decode the next ASN.1 element as a bit string.
        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
      • decodePrintableString

        String decodePrintableString()
                              throws ASN1Exception
        Decode the next ASN.1 element as a PrintableString.
        Returns:
        the decoded PrintableString as a string
        Throws:
        ASN1Exception - if the next element is not a PrintableString
      • decodePrintableStringAsBytes

        byte[] decodePrintableStringAsBytes()
                                     throws ASN1Exception
        Decode the next ASN.1 element as a PrintableString.
        Returns:
        the decoded PrintableString as a byte array
        Throws:
        ASN1Exception - if the next element is not a PrintableString
      • decodeUniversalString

        default String decodeUniversalString()
                                      throws ASN1Exception
        Decode the next ASN.1 element as an UniversalString.
        Returns:
        the decoded UniversalString as a string
        Throws:
        ASN1Exception - if the next element is not a UniversalString
        UnsupportedOperationException - if this implementation does not provide this method
      • decodeUniversalStringAsBytes

        default byte[] decodeUniversalStringAsBytes()
                                             throws ASN1Exception
        Decode the next ASN.1 element as an UniversalString.
        Returns:
        the decoded UniversalString as a byte array
        Throws:
        ASN1Exception - if the next element is not a UniversalString
        UnsupportedOperationException - if this implementation does not provide this method
      • decodeUtf8String

        default String decodeUtf8String()
                                 throws ASN1Exception
        Decode the next ASN.1 element as an UTF8String.
        Returns:
        the decoded UTF8String as a string
        Throws:
        ASN1Exception - if the next element is not a UTF8String
        UnsupportedOperationException - if this implementation does not provide this method
      • decodeUtf8StringAsBytes

        default byte[] decodeUtf8StringAsBytes()
                                        throws ASN1Exception
        Decode the next ASN.1 element as an UTF8String.
        Returns:
        the decoded UTF8String as a byte array
        Throws:
        ASN1Exception - if the next element is not a UTF8String
        UnsupportedOperationException - if this implementation does not provide this method
      • decodeBMPString

        default String decodeBMPString()
                                throws ASN1Exception
        Decode the next ASN.1 element as an BMPString.
        Returns:
        the decoded BMPString as a string
        Throws:
        ASN1Exception - if the next element is not a BMPString
        UnsupportedOperationException - if this implementation does not provide this method
      • decodeBMPStringAsBytes

        default byte[] decodeBMPStringAsBytes()
                                       throws ASN1Exception
        Decode the next ASN.1 element as an BMPString.
        Returns:
        the decoded BMPString as a byte array
        Throws:
        ASN1Exception - if the next element is not a BMPString
        UnsupportedOperationException - if this implementation does not provide this method
      • decodeObjectIdentifier

        String decodeObjectIdentifier()
                               throws ASN1Exception
        Decode the next ASN.1 element as an object identifier.
        Returns:
        the object identifier as a string
        Throws:
        ASN1Exception - if the next element is not a bit string
      • decodeInteger

        BigInteger decodeInteger()
        Decode the next ASN.1 element as an integer.
        Returns:
        an integer decoded from the next element
        Throws:
        ASN1Exception - if the next element is not an integer
      • decodeNull

        void decodeNull()
                 throws ASN1Exception
        Decode the next ASN.1 element as a null element.
        Throws:
        ASN1Exception - if the next element is not null
      • decodeImplicit

        void decodeImplicit​(int number)
        Indicate that the next ASN.1 element has the given implicit, context-specific tag.
        Parameters:
        number - the tag number for the implicit tag
      • decodeImplicit

        void decodeImplicit​(int clazz,
                            int number)
        Indicate that the next ASN.1 element has the given implicit tag.
        Parameters:
        clazz - the class for the implicit tag
        number - the tag number for the implicit tag
      • decodeBoolean

        boolean decodeBoolean()
                       throws ASN1Exception
        Decode the next ASN.1 element as a boolean value.
        Returns:
        the decoded boolean value
        Throws:
        ASN1Exception - if the next element is not a boolean value
        Since:
        1.2.0
      • isNextType

        boolean isNextType​(int clazz,
                           int number,
                           boolean isConstructed)
        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.
        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

        int peekType()
              throws ASN1Exception
        Retrieve the type of the next ASN.1 element without actually decoding the next element.
        Returns:
        the type of the next ASN.1 element
        Throws:
        ASN1Exception - if an error occurs while determining the type of the next element
      • skipElement

        void skipElement()
                  throws ASN1Exception
        Skip over the next ASN.1 element.
        Throws:
        ASN1Exception - if the next element cannot be skipped
      • hasNextElement

        boolean hasNextElement()
        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.
        Returns:
        true if there is at least one more ASN.1 element that can be read and false otherwise
      • drainElementValue

        byte[] drainElementValue()
                          throws ASN1Exception
        Drain the value bytes from the next ASN.1 element.
        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

        byte[] drainElement()
                     throws ASN1Exception
        Drain all of the bytes from the next ASN.1 element.
        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

        byte[] drain()
        Drain all of the remaining bytes from the input stream.
        Returns:
        all of the remaining bytes from the input stream