public abstract class ConstructedType extends ASN1Object
Within the ASN.1 architecture of the IAIK-JCE library, this ConstructedType
class rather models ASN.1 types which have to be encoded constructed, and not only
native structured ASN.1 types. The ASN.1 type OCTET_STRING,
for instance, in its nature represents a simple ASN.1 type and therefore has to be DER
encoded by using the primitive definite method where the number of data octets
explicitly has to be specified just behind the identifier octet. However sometimes it
may be preferable to use the indefinite constructed method for BER encoding an
octet string, in particular when dealing with large amounts of raw data. For that reason,
also the OCTET_STRING class inherits from this ConstructedType class.
Indefinite length encoding is indicated by the length octet 0x80, and the data octets
are concluded by two consecutive octets of all zero (0x00 0x00).
As an example, the raw data bytes 0x12 0xAB 0x34 may be encoded definite primitive,
e.g.:
0x04 0x03 0x12 0xAB 0x34or they may be encoded indefinite constructed, e.g.:
0x24 0x80
0x04 0x02 0x12 0xAB
0x04 0x01 0x34
0x00 0x00
An application never directly will instantiate this ConstructedType class,
rather it will instantiate a proper subclass, e.g. SEQUENCE
or SET. Components may be added by means of a proper
addComponent method, e.g.:
byte[] value = {(byte)0x12, (byte)0xAB, (byte)0x34 };
OCTET_STRING oct = new OCTET_STRING(value);
INTEGER i = new INTEGER(3);
SEQUENCE seq = new SEQUENCE();
seq.addComponent(oct);
seq.addComponent(i);
A component may be removed by using one of the removeComponent methods, or they may be replaced by calling the
setComponent method.
For building SEQUENCE OF respectively SET OF structures of
ASN1Type implementing class instances, use the
createSequenceOf respectively
createSetOf methods of the
ASN class.
SEQUENCE,
SET,
OCTET_STRING,
ASN1Object| Modifier and Type | Field and Description |
|---|---|
protected int |
content_count
The number of components in the buffer.
|
protected ASN1Object[] |
content_data
The buffer where the components are stored.
|
asnType, constructed, encode_listener, indefinite_length, isStringType, stream_mode| Constructor and Description |
|---|
ConstructedType()
Creates a new ConstructedType object.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addComponent(ASN1Object component)
Adds a new ASN1Object component to the end of this ConstructedType.
|
void |
addComponent(ASN1Object component,
int index)
Inserts a new component to a given location within the ConstructedType.
|
void |
addEncodeListener(EncodeListener encodeListener,
int id,
int afterComponent)
Adds a component encode listener which wants to be notified when the encoding of this
constructed ASN1Object has to be performed.
|
java.lang.Object |
clone()
Returns a clone of this ConstructedType.
|
int |
countComponents()
Returns the number of component objects.
|
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 ASN1Object and writes the result to the supplied OutputStream.
|
ASN1Object |
getComponentAt(int index)
Returns the component at the given location within a ConstructedType.
|
java.util.Enumeration |
getComponents()
Returns an Enumeration that iterates over the component objects.
|
java.lang.Object |
getValue()
Returns the value of this ConstructedType as an array of ASN1Objects.
|
void |
removeComponent(ASN1Object component)
Removes a component from a ConstructedType.
|
void |
removeComponent(int index)
Removes a component at the given location within a ConstructedType.
|
void |
setComponent(int index,
ASN1Object value)
Sets the component at the given location within a ConstructedType.
|
void |
setValue(java.lang.Object object)
Sets the value of this ConstructedType.
|
addEncodeListener, encodeObject, getAsnType, indefiniteLength, isA, isConstructed, isStringType, setIndefiniteLength, toStringprotected ASN1Object[] content_data
protected int content_count
public ConstructedType()
addComponent methods.
An application will not directly use this constructor for creating
a constructed ASN1Object, rather it will instantiate a proper subclass,
e.g. SEQUENCE or SET.public java.lang.Object clone()
clone in class ASN1Objectpublic java.lang.Object getValue()
OCTET_STRING may change the exact meaning
of this method according to specific requirements.getValue in class ASN1Objectpublic void setValue(java.lang.Object object)
OCTET_STRING may change the exact meaning
of this method according to specific requirements.setValue in class ASN1Objectobject - the value to be set as an array of ASN1Objectspublic void addComponent(ASN1Object component)
addComponent in class ASN1Objectcomponent - the ASN1Object component which should be added to the
ConstructedTypeConstructedTypepublic void addComponent(ASN1Object component, int index)
component - the ASN1Object component to be insertedindex - the index where to insert the new componentjava.lang.ArrayIndexOutOfBoundsException - if the index is invalidpublic void removeComponent(ASN1Object component)
component - the ASN1Object component to be removedpublic void removeComponent(int index)
index - the index of the component to be removedjava.lang.ArrayIndexOutOfBoundsException - if the index is invalidpublic ASN1Object getComponentAt(int index)
getComponentAt in class ASN1Objectindex - the desired positionjava.lang.ArrayIndexOutOfBoundsException - if the index is invalidConstructedTypepublic void setComponent(int index,
ASN1Object value)
index - the location of the ConstructedType to which the
given ASN1Object shall be writtenvalue - the ASN1Object component to be setjava.lang.ArrayIndexOutOfBoundsException - if the index is invalidpublic int countComponents()
countComponents in class ASN1ObjectConstructedTypepublic java.util.Enumeration getComponents()
protected void encode(java.io.OutputStream os)
throws java.io.IOException
encode in class ASN1Objectos - the output stream to which to write the datajava.io.IOException - if an error occurs while reading from the streamprotected void decode(int length,
java.io.InputStream is)
throws CodingException,
java.io.IOException
length bytes represent the encoding of this
ASN1Object to be decoded. If length < 0,
the supplied ASN1Object 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 ASN1Object. 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
ASN1Object and internally calls the decode
method of that ASN1Object.
decode in class ASN1Objectlength - the length of the ASN1Object which shall be decoded, i.e. the
number of the bytes representing the ASN1Object to be decodedis - the input stream from which the data is read injava.io.IOException - if an error occurs while reading from the streamCodingException - if a decoding error occurspublic void addEncodeListener(EncodeListener encodeListener, int id, int afterComponent)
EncodeListener
interface. The actual registration for encoding notification is done when
calling an addEncodeListener method on a particular ASN1Object.
Later, when the encoding of the current ASN1Object is performed, the program
execution jumps to the encodeCalled method of each registered EncodeListener implementation for querying
for information that is needed for the encoding procedure. After having executed the
encodeCalled methods of all registered listeners, the particular
encode method of the ASN1Object is executed. In contrast to the
addEncodeListener
method of parent class ASN1Object, for an constructed
now you can specify the component after which the encoding process shall "pause" to
get the values for the remaining components. Imagine, for instance, a SEQUENCE
being composed of five components for which you do not know the values of
the last 3 components actually before the first components have been encoded.
In this case you may register an EncodeListener to execute its encodeCalled
method after 2 components have been encoded, e.g.:
SEQUENCE seq = ...; seq.addEncodeListener(myEncodeListener, 1, 2);Attention! Adding an component encode listener only makes sense if this constructed ASN1Object is to be encoded using stream mode.
encodeListener - the EncodeListenerid - an id which lets listeners distinguish between objectsafterComponent - the number of component after which the EncodeListener should
be calledEncodeListener