public abstract class TaggedType extends ConstructedType
ASN.1 types are unequivocally identified by their tags. Every
ASN.1 type - except for ANY and CHOICE types - is allocated a particular tag
specifying the tag class to which this type belongs and the
tag number which separates the current ASN.1 type from the
other types belonging to the same tag class. ASN.1 defines four
tag classes:
A tagged ASN.1 type is any ANS.1 type with a tag from the Application, Context-specific, or Private tag class. An ASN.1 type with a tag from the Universal class is either a simple or constructed type. This class will be the super class for classes that represent the Application tagged ASN.1 type, the Context-specific tagged type, and the Private tagged ASN.1 type.
For BER (respectively DER) encoding a tagged type which is derived from some underlying type, either EXPLICIT or IMPLICIT tagging may be used. The tag of the underlying type explicitly has to be encoded when EXPLICIT tagging is used. IMPLICIT tagging avoids the extra encoding of the tag of the underlying type and may be used when the actual tag can be derived from the context.
Explicit tagging always enforces constructed encoding, and the contents consists of the full encoding of the underlying base type. When implicit tagging is required, the kind of encoding (primitive or constructed) depends on the base type: if the base type is a simple type, the tagged type is primitive encoded, but if the base type is constructed the tagged type also has to be encoded constructed.
ASN1Object,
ASN,
DerCodercontent_count, content_dataasnType, constructed, encode_listener, indefinite_length, isStringType, stream_mode| Modifier and Type | Method and Description |
|---|---|
protected void |
decode(int length,
java.io.InputStream is)
Decodes the next available data from the InputStream.
|
protected void |
encode(java.io.OutputStream os)
DER encodes this TaggedType ASN1Object and writes the result to the given
output stream.
|
void |
forceImplicitlyTagged(ASN type)
Forces a tagged ASN.1 object to be implicitly tagged.
|
java.lang.Object |
getValue()
Returns the value of this TaggedType as an ASN1Object.
|
boolean |
isImplicitlyTagged()
Returns
true if this tagged ASN1Object is
implicitly tagged. |
java.lang.String |
toString()
Returns a string that represents the contents of this tagged ASN1Object.
|
addComponent, addComponent, addEncodeListener, countComponents, getComponentAt, getComponents, removeComponent, removeComponent, setComponent, setValueaddEncodeListener, encodeObject, getAsnType, indefiniteLength, isA, isConstructed, isStringType, setAutomaticRepair, setAutomaticRepairRecursive, setIndefiniteLength, setIndefiniteRecursivepublic java.lang.Object getValue()
getValue in class ConstructedTypepublic boolean isImplicitlyTagged()
true if this tagged ASN1Object is
implicitly tagged.true if implicitly tagged, false if
explicitly taggedpublic void forceImplicitlyTagged(ASN type) throws CodingException
There is no way for the DER decoder to detect if a tagged ASN.1 object is
Example (using a Context-specific tagged type):
problem [0] EXPLICIT InnerObject;
InnerObject ::= INTEGER;
problem [0] IMPLICIT InnerObject;
InnerObject ::= SEQUENCE {
test INTEGER;
}
Both examples lead to the same ASN.1 DER encoding:
A0 03 02 01 01 if test = 1
Only the application which parses the ASN1Object knows if an implicitly or an explicitly tagged type defined. The solution for this problem looks like:
ASN1Object problem = ...; problem.forceImplicitlyTagged(ASN.SEQUENCE);
Constructed tagged ASN.1 objects are always decoded as explicitly tagged per default.
type - the type of the lost implicitly tagged typeCodingException - if no instance of the type can be createdprotected void encode(java.io.OutputStream os)
throws java.io.IOException
encode methods of the
DerCoder class for performing the encoding.
The encode method of this TaggedType class will be called
internally from the DerCoder.encode in class ConstructedTypeos - the output stream to which to write the datajava.io.IOException - if an error occurs while writing to the streamprotected void decode(int length,
java.io.InputStream is)
throws java.io.IOException,
CodingException
The supplied input stream carries DER encoded data. The next
length bytes represent the encoding of this tagged ASN.1
object to be decoded. If length = 0, the supplied tagged
ASN.1 type has been encoded by means of the indefinite length encoding
method, and so the encoding has to be parsed for two consecutive EOC
octets of all zeros. Indefinite length encoding only is appropriate (and
therefore allowed) for constructed ASN.1 types.
This is a protected method and will not be used by an application for DER
decoding an encoded TaggedType. An application will call one of the
decode methods of the DerCoder
class for performing the decoding. The DerCoder then determines the
number of bytes (length) obtained by the particular
TaggedType and internally calls the decode method of that
TaggedType instance.
decode in class ConstructedTypelength - the length, i.e. number of the bytes representing the
ASN1Object to be decodedis - the input stream from which the DER encoded data is read injava.io.IOException - if there is a problem with the InputStreamCodingException - if the bytes from is cannot be decodedpublic java.lang.String toString()
toString in class ASN1ObjectASN1Object.toString()