public class CON_SPEC extends TaggedType
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:
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.
Since explicit tagging is the default value and requires constructed
encoding, this 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 specifiic 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:02
For comparison, if the cotext specific tagged 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:02
Attention 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 tagged component of the first SEQUENCE shall be explicitly tagged and shall represent a simple OCTET_STRING component. The context specific tagged 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 = 2
Second SEQUENCE:
SEQUENCE[C] = 3 elements
INTEGER = 1
CONTEXTSPECIFIC[C] = [0] IMPLICIT
SEQUENCE[C] = 1 elements
OCTET STRING = 2 bytes: AB:CD
INTEGER = 2
The encoding will be the same, since the context specific tagged 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: constrcuted (1), tag number: 0 (00000). The context specific tagged 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:02
However the application that parses the received SEQUENCE object knows that
implictly 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,
DerCodercontent_count, content_dataasnType, 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 tagged ASN.1 type.
|
|
CON_SPEC(int conSpecTag,
ASN1Object obj,
boolean implicitlyTagged)
Creates a new context specific tagged ASN.1 type.
|
| Modifier and Type | Method and Description |
|---|---|
static OCTET_STRING |
parseImplicitContextSpecificTaggedOctetString(ASN1Object obj,
int expectedTag,
java.lang.String objName)
Ensures that the ASN.1 object is an [CONTEXT SPECIFIC] IMPLICIT OCTET
STRING with the indicated value.
|
decode, encode, forceImplicitlyTagged, getValue, isImplicitlyTagged, toStringaddComponent, addComponent, addEncodeListener, countComponents, getComponentAt, getComponents, removeComponent, removeComponent, setComponent, setValueaddEncodeListener, encodeObject, getAsnType, indefiniteLength, isA, isConstructed, isStringType, setAutomaticRepair, setAutomaticRepairRecursive, setIndefiniteLength, setIndefiniteRecursiveprotected 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 static OCTET_STRING parseImplicitContextSpecificTaggedOctetString(ASN1Object obj, int expectedTag, java.lang.String objName) throws CodingException
obj - an ASN.1 objectexpectedTag - the tag value that is expectedobjName - a string name for this ASN.1 object (used in excpetion
messages)CodingException - if the ASN.1 object is not an [CONTEXT SPECIFIC] IMPLICIT
OCTET STRING with the indicated value