Class Asn1Tag


  • public final class Asn1Tag
    extends Object
    Provides methods for building and analyzing ASN.1 tag bytes.

    Examples of usage:

    • Building a Universal, primitive with number 3 tag:
      Asn1Tag.universal().number(3);
      will return 0x03.
    • Building an application, constructed with number 9 tag:
      Asn1Tag.application().constructed().number(9);
      will return 0x69.
    • Field Detail

      • UNIVERSAL_BOOLEAN_TYPE

        public static final byte UNIVERSAL_BOOLEAN_TYPE
        The BER type that is assigned to the universal Boolean element.
        See Also:
        Constant Field Values
      • UNIVERSAL_INTEGER_TYPE

        public static final byte UNIVERSAL_INTEGER_TYPE
        The BER type that is assigned to the universal integer type.
        See Also:
        Constant Field Values
      • UNIVERSAL_BIT_STRING_TYPE

        public static final byte UNIVERSAL_BIT_STRING_TYPE
        The BER type that is assigned to the universal bit string type.
        See Also:
        Constant Field Values
      • UNIVERSAL_OCTET_STRING_TYPE

        public static final byte UNIVERSAL_OCTET_STRING_TYPE
        The BER type that is assigned to the universal octet string type.
        See Also:
        Constant Field Values
      • UNIVERSAL_NULL_TYPE

        public static final byte UNIVERSAL_NULL_TYPE
        The BER type that is assigned to the universal null type.
        See Also:
        Constant Field Values
      • UNIVERSAL_OBJECT_IDENTIFIER

        public static final byte UNIVERSAL_OBJECT_IDENTIFIER
        The BER type that is assigned to the universal object identifier type.
        See Also:
        Constant Field Values
      • UNIVERSAL_ENUMERATED_TYPE

        public static final byte UNIVERSAL_ENUMERATED_TYPE
        The BER type that is assigned to the universal enumerated type.
        See Also:
        Constant Field Values
      • UNIVERSAL_UTF8_STRING_TYPE

        public static final byte UNIVERSAL_UTF8_STRING_TYPE
        The BER type that is assigned to the universal UTF-8 String type.
        See Also:
        Constant Field Values
      • UNIVERSAL_PRINTABLE_STRING_TYPE

        public static final byte UNIVERSAL_PRINTABLE_STRING_TYPE
        The BER type that is assigned to the universal Printable String type.
        See Also:
        Constant Field Values
      • UNIVERSAL_UTC_TIME_TYPE

        public static final byte UNIVERSAL_UTC_TIME_TYPE
        The BER type that is assigned to the utc time type.
        See Also:
        Constant Field Values
      • UNIVERSAL_GENERALIZED_TIME_TYPE

        public static final byte UNIVERSAL_GENERALIZED_TIME_TYPE
        The BER type that is assigned to the generalized time type.
        See Also:
        Constant Field Values
      • UNIVERSAL_SEQUENCE_TYPE

        public static final byte UNIVERSAL_SEQUENCE_TYPE
        The BER type that is assigned to the universal sequence type.
        See Also:
        Constant Field Values
      • UNIVERSAL_SET_TYPE

        public static final byte UNIVERSAL_SET_TYPE
        The BER type that is assigned to the universal set type.
        See Also:
        Constant Field Values
    • Method Detail

      • valueOf

        public static Asn1Tag valueOf​(byte tag)
        Converts the provided single byte tag to an Asn1Tag.
        Parameters:
        tag - The single byte tag to be converted.
        Returns:
        The Asn1Tag corresponding to the provided tag byte.
      • valueOf

        public static Asn1Tag valueOf​(long tag)
        Converts the provided multi-byte tag to an Asn1Tag.
        Parameters:
        tag - The multi-byte tag to be converted.
        Returns:
        The Asn1Tag corresponding to the provided tag byte.
      • universal

        public static Asn1Tag universal()
        Returns a primitive ASN.1 tag with the universal tag class.
        Returns:
        A primitive ASN.1 tag with the universal tag class.
      • application

        public static Asn1Tag application()
        Returns a primitive ASN.1 tag with the application tag class.
        Returns:
        A primitive ASN.1 tag with the application tag class.
      • contextSpecific

        public static Asn1Tag contextSpecific()
        Returns a primitive ASN.1 tag with the context-specific tag class.
        Returns:
        A primitive ASN.1 tag with the context-specific tag class.
      • privateTag

        public static Asn1Tag privateTag()
        Returns a primitive ASN.1 tag with the private tag class.
        Returns:
        A primitive ASN.1 tag with the private tag class.
      • constructed

        public Asn1Tag constructed()
        Changes this ASN.1 tag from primitive to constructed.
        Returns:
        this ASN.1 tag.
      • number

        public byte number​(int tagNumber)
        Returns the single byte representation of this tag having the provided tag number. The tag number MUST be less than 31 in order to fit into a single byte. Use numberMultiByte(long) for handling multi-byte tag numbers.
        Parameters:
        tagNumber - The tag number which must be less than 31.
        Returns:
        The byte representation of this tag having the provided tag number.
        Throws:
        IllegalArgumentException - If the tag number was greater than 30 and requires a multi-byte encoding.
      • numberMultiByte

        public long numberMultiByte​(long tagNumber)
        Returns the multi-byte representation of this tag having the provided tag number encoded as a long which may then be used for encoding and decoding multi-byte ASN.1 tags. The encoded tag is returned as a long whose lowest bytes contain the reverse ASN.1 encoding of the tag, such that the lowest byte contains the class and form. This ensures that comparisons with single byte encodings work as expected. The tag number MUST be less than 2^50.
        Parameters:
        tagNumber - The tag number which must be less than 2^50.
        Returns:
        The long representation of this tag having the provided tag number.
        Throws:
        IllegalArgumentException - If the tag number was greater than 2^50.
      • isConstructed

        public boolean isConstructed()
        Returns whether this tag is constructed.
        Returns:
        true if this tag is constructed, false if it is primitive.
      • getTagClass

        public Asn1Tag.Class getTagClass()
        Returns the tag class.
        Returns:
        The tag class.
      • getTagNumber

        public long getTagNumber()
        Returns the tag number.
        Returns:
        the tag number.
      • toString

        public String toString()
        Returns the string representation of this Asn1 tag. The string value corresponds to the chain of methods that should be called to produce the same Asn1 tag, e.g. "contextSpecific().constructed().number(3)".
        Overrides:
        toString in class Object
        Returns:
        The string representation of this Asn1 tag.