public class CON_SPEC extends ConstructedType
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
:
CON_SPEC
class inherits from the
ConstructedType
class. Implicit tagging may
be enforced by setting the implicitlyTagged
parameter to
true
when creating a new CON_SPEC object by means of the
CON_SPEC(int conSpecTag,
ASN1Object obj, boolean implicitlyTagged)
constructor. The following example
creates an ASN.1 SEQUENCE object consisting of two INTEGER components, and
one OCTET_STRING component, which is tagged context specific implicit
with tag number 0:
INTEGER i1 = new INTEGER(1); OCTET_STRING oct = new OCTET_STRING(new byte[] { (byte) 0xAB, (byte) 0xCD }); INTEGER i2 = new INTEGER(2); SEQUENCE seq = new SEQUENCE(); seq.addComponent(i1); seq.addComponent(new CON_SPEC(0, oct, true)); seq.addComponent(i2);The example above will give the following DER encoding:
30:0A:02:01:01:80:02:AB:CD:02:01:02For comparison, if the cotext specific OCTET_STRING component of the SEQUENCE above is tagged explicitly, it will be encoded constructed and OCTET_STRING tag (0x04) and length are included in the encoding:
30:0C:02:01:01:A0:04:04:02:AB:CD:02:01:02Attention has to be paid when implicitly tagging a structured component, e.g. a SEQUENCE component. Since the SEQUENCE tag (0x30) is not included into the encoding, particular care has to be taken when decoding and parsing such a structure. Consider, for instance, two ASN.1 SEQUENCE structures, both containing a context specific tagged component with the same tag number 0. The context specific component of the first SEQUENCE shall be explicitly tagged and shall represent a simple OCTET_STRING component. The context specific component of the second SEQUENCE object shall be implicitly tagged and shall represent a SEQUENCE component containing one OCTET_STRING component with the same value as the OCTET_STRING component of the first SEQUENCE object:
First SEQUENCE:
SEQUENCE[C] = 3 elements INTEGER = 1 CONTEXTSPECIFIC[C] = [0] EXPLICIT OCTET STRING = 2 bytes: AB:CD INTEGER = 2Second SEQUENCE:
SEQUENCE[C] = 3 elements INTEGER = 1 CONTEXTSPECIFIC[C] = [0] IMPLICIT SEQUENCE[C] = 1 elements OCTET STRING = 2 bytes: AB:CD INTEGER = 2The encoding will be the same, since the context specific component of the first SEQUENCE object is explicitly tagged and therefore constructed encoded giving a tag of 0xA0 (10100000: tag class: context specific (10), encoding type: constructed (1), tag number: 0 (00000). The context specific component of the second SEQUENCE object is implicitly tagged and therefore the encoding type (primitive or constructed) is derived from the base type, which is a SEQUENCE and therefore gives the same context specific tag as in the first example: 0xA0. Since implicit tagging does not encode the tag of the base type (SEQUENCE 0x30), the encoding result is the same for both SEQUENCE objects:
30:0C:02:01:01:A0:04:04:02:AB:CD:02:01:02However the application that parses the received SEQUENCE object knows that implicitly tagging has been used and therefore can recognize the right encoding by using the
forceImplicitlyTagged
method:
SEQUENCE obtained_sequence = (SEQUENCE) DerCoder.decode(encoding); CON_SPEC cs = (CON_SPEC) obtained_sequnce.getComponentAt(1); cs.forceImplicitlyTagged(ASN.SEQUENCE); SEQUENCE seq_component = (SEQUENCE) cs.getValue(); OCTET_STRING oct = (OCTET_STRING) seq_component.getComponentAt(0);
ASN1Object
,
ASN
,
DerCoder
content_count, content_data
asnType, constructed, encode_listener, indefinite_length, isStringType, stream_mode
Modifier | Constructor and Description |
---|---|
protected |
CON_SPEC()
Creates an empty CON_SPEC object.
|
|
CON_SPEC(int conSpecTag,
ASN1Object obj)
Creates a new explicitly tagged context specific ASN.1 type.
|
|
CON_SPEC(int conSpecTag,
ASN1Object obj,
boolean implicitlyTagged)
Creates a new context specific ASN.1 type.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Object |
clone()
Returns a clone of this CON_SPEC.
|
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 CON_SPEC ASN1Object and writes the result to the given
output stream.
|
void |
forceImplicitlyTagged(ASN type)
Forces a CONTEX SPECIFIC ASN.1 object to be implicitly tagged.
|
java.lang.Object |
getValue()
Returns the value of this CON_SPEC as an ASN1Object.
|
boolean |
isImplicitlyTagged()
Returns
true if this Context Specific ASN1Object is implicitly
tagged. |
java.lang.String |
toString()
Returns a string that represents the contents of this CON_SPEC ASN1Object.
|
addComponent, addComponent, addEncodeListener, countComponents, getComponentAt, getComponents, removeComponent, removeComponent, setComponent, setValue
addEncodeListener, encodeObject, getAsnType, indefiniteLength, isA, isConstructed, isStringType, setIndefiniteLength
protected CON_SPEC()
public CON_SPEC(int conSpecTag, ASN1Object obj)
#CON_SPEC(int, ASN1Object,
boolean)
constructor for enforcing implicit tagging.conSpecTag
- the context specific tag numberobj
- the underlying ASN1Objectpublic CON_SPEC(int conSpecTag, ASN1Object obj, boolean implicitlyTagged)
conSpecTag
- the context specific tag numberobj
- the underlying ASN1ObjectimplicitlyTagged
- true
if creating an implicitly tagged object,
false
if creating an explicitly tagged objectpublic java.lang.Object clone()
clone
in class ConstructedType
public java.lang.Object getValue()
getValue
in class ConstructedType
public boolean isImplicitlyTagged()
true
if this Context Specific 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 an context specific ASN.1 object is
Example:
problem [0] EXPLICIT InnerObject; InnerObject ::= INTEGER;Both examples lead to the same ASN.1 DER encoding:problem [0] IMPLICIT InnerObject; InnerObject ::= SEQUENCE { test INTEGER; }
A0 03 02 01 01 if test = 1Only the application which parses the ASN1Object knows if an implicitly or an explicitly tagged context specific was defined.
The solution for this problem looks like:
ASN1Object problem = ...; problem.forceImplicitlyTagged(ASN.SEQUENCE);Constructed context specific 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 CON_SPEC class will be called internally from the DerCoder.encode
in class ConstructedType
os
- 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
length
bytes
represent the encoding of this CON_SPEC object to be decoded. If
length < 0
, the supplied CON_SPEC 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 CON_SPEC. 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 CON_SPEC and
internally calls the decode
method of that CON_SPEC instance.
decode
in class ConstructedType
length
- 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 ASN1Object
ASN1Object.toString()