iaik.cms
Class Data

java.lang.Object
  extended by iaik.cms.DataStream
      extended by iaik.cms.Data
All Implemented Interfaces:
Content, ContentStream

public class Data
extends DataStream
implements Content

This class represents the non-stream implementation of the CMS content type Data.

Each CMS content type is associated with a specific object identifier, derived from:

 pkcs-7 OBJECT IDENTIFIER ::=
   { iso(1) member-body(2) US(840) rsadsi(113549)
       pkcs(1) 7 }
 

The object identifier for the Data content type is defined as:

data OBJECT IDENTIFIER ::= { pkcs-7 1 }

which corresponds to the OID string "1.2.840.113549.1.7.1".

The Cryptographic Message Syntax (CMS) (RFC 5652) specifies the Data content type as base type without any cryptographic enhancements. Please note that in difference to PKCS#7, CMS specifies the Data as arbitrary octet strings, such as ASCII text files; the interpretation is left to the application. From this point of view the CMS type Data itself does not have an ASN.1 respresentation in contrast to its PKCS#7 equivalent where Data is defined as ASN.1 OCTET STRING:

Data ::= OCTET STRING

This class only may be used for compatibility to PKCS#7 for allowing to include Data objects into ContentInfos. Within SignedData or EnvelopedData objects the internal content always is wrapped by an EncapsulatedContentInfo object. Since an EncapsulatedContentInfo anytime is encoded as OCTET STRING backwards compatibility to PKCS#7 is preserved even when the inner content type is Data

Use the Data(byte[] content) constructor to create a new Data object for the given raw data bytes:

 byte[] value = ...;
 Data data = new Data(value);
 
Some applications use the indefinite constructed encoding method for generating an octet string that is composed of a certain number of definite primitive encoded octet strings:
 0x24 0x80
           0x04 <blocksize> <data>
           0x04 <blocksize> <data>
           0x04 <blocksize> <data>
                ...
 0x00 0x00
 
For being compatible to such applications, this class provides a constructor where a particular blocksize may be specified to give an encoding result as described above:
 Data data = new Data(value, blockSize);
 

In contrast to the stream-variant of the CMS Data type (implemented by the DataStream), where the raw data is supplied from an input stream and therefore can be read only once, it may be obtained arbitrarily often from a non-stream Data object by utilizing the getData method.

After transforming a Data object to an ASN1Object by means of the toASN1Object method, it may be parsed back again by using the Data(ASN1Object obj) constructor. In general, the getEncoded method returns this Data object as definite primitive encoded octet string:

 0x04 <length> <data>
 
However, if a positive blocksize has been specified when creating this Data object, the encoding will be indefinite constructed.

For handling large amounts of data, the parent DataStream class should be used.

See Also:
Content, ContentInfo

Field Summary
 
Fields inherited from class iaik.cms.DataStream
blockSize_, inputStream_
 
Constructor Summary
protected Data()
          Default constructor for dynamic object creation in ContentInfo.
  Data(ASN1Object obj)
          Creates a CMS Data object from an ASN1Object.
  Data(byte[] content)
          Creates a CMS Data object from a byte array supplying the data value.
  Data(byte[] content, int blockSize)
          Creates a CMS Data object from a byte array supplying the data value and a blockSize specifying the encoding scheme.
  Data(java.io.InputStream is)
          Creates a new CMS Data from a DER encoded Data object read from the given input stream.
 
Method Summary
 void decode(ASN1Object obj)
          Reads and decodes the Data from an ASN1Object.
 void decode(java.io.InputStream is)
          Reads and decodes the DER encoded Data from the given input stream.
 byte[] getData()
          Returns a byte array holding the value of this Data object.
 byte[] getEncoded()
          Returns the DER encoding of this Data object as byte array.
 java.io.InputStream getInputStream()
          Returns an InputStream from which the contents of this object can be read.
 ASN1Object toASN1Object()
          Returns this CMS Data as ASN1Object.
 java.lang.String toString(boolean detailed)
          Returns a string giving some - if requested - detailed information about this Data object.
 
Methods inherited from class iaik.cms.DataStream
getBlockSize, getContentType, setBlockSize, toString, writeTo
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface iaik.cms.ContentStream
getBlockSize, getContentType, setBlockSize
 

Constructor Detail

Data

protected Data()
Default constructor for dynamic object creation in ContentInfo. Shall be not used by an application. The block size is set to -1 to enforce definite primitive encoding.


Data

public Data(byte[] content)
Creates a CMS Data object from a byte array supplying the data value.

Use this constructor for supplying the content value, e.g.:

 Data data = new Data("Test data".getBytes());
 

Parameters:
content - the data value in a byte array

Data

public Data(byte[] content,
            int blockSize)
Creates a CMS Data object from a byte array supplying the data value and a blockSize specifying the encoding scheme. If blockSize is not positive this Data object will be encoded as definite primitive octet string:
 0x04 <length> <data>
 
If blockSize is positive this Data object will be encoded as indefinite constructed octet string being composed of a certain number of definite primitive octet strings of blockSize length:
 0x24 0x80
           0x04 <blocksize> <data>
           0x04 <blocksize> <data>
           0x04 <blocksize> <data>
                ...
 0x00 0x00
 

Parameters:
content - the data value in a byte array
blockSize - for defining the encoding scheme - and specifiying the octet string component length, if positive

Data

public Data(ASN1Object obj)
     throws CMSParsingException
Creates a CMS Data object from an ASN1Object. The ASN.1 Data may (or may not) be wrapped into a ContentInfo. Do not use this constructor for supplying the content value. This constructor may be used for parsing an already exisiting Data object, supplied as ASN1Object that may have been created by calling toASN1Object.

Use the Data(byte[] content) constructor for supplying the content value when creating a Data object.

Parameters:
obj - the CMS Data as ASN1Object
Throws:
CMSParsingException - if an parsing error occurs

Data

public Data(java.io.InputStream is)
     throws java.io.IOException,
            CMSParsingException
Creates a new CMS Data from a DER encoded Data object read from the given input stream. The encoded Data may (or may not) be wrapped into a ContentInfo. Do not use this constructor for supplying the content value. This constructor may be used for decoding and parsing an already exisiting Data object, supplied as DER encoding from an input stream.

Parameters:
is - the input stream supplying the DER encoded Data
Throws:
java.io.IOException - if an I/O error occurs during reading from the InputStream
CMSParsingException - if an error occurs while parsing the object
Method Detail

decode

public void decode(java.io.InputStream is)
            throws java.io.IOException,
                   CMSParsingException
Reads and decodes the DER encoded Data from the given input stream. The encoded Data may (or may not) be wrapped into a ContentInfo.

Specified by:
decode in interface ContentStream
Overrides:
decode in class DataStream
Parameters:
is - the InputStream holding a DER encoded CMS Data object
Throws:
java.io.IOException - if an I/O error occurs during reading from the InputStream
CMSParsingException - if an error occurs while parsing the object

decode

public void decode(ASN1Object obj)
            throws CMSParsingException
Reads and decodes the Data from an ASN1Object. The ASN.1 Data may (or may not) be wrapped into a ContentInfo.

Specified by:
decode in interface Content
Parameters:
obj - the CMS Data object as ASN1Object
Throws:
CMSParsingException - if an error occurs while parsing the object

getData

public byte[] getData()
Returns a byte array holding the value of this Data object.

Returns:
the value of this Data object, as byte array

getInputStream

public java.io.InputStream getInputStream()
Returns an InputStream from which the contents of this object can be read.

This method only overrides the corresponding getInputStream method of the parent DataStream class for returning the content of this Data object. There should be no real necessity for using this method since the raw data bytes immediately can be obtained by the getData method. However, in contrast to the equivalent getInputStream method of the parent DataStream class, this method may be called arbitrarly often; it only returns a ByteArrayInputStream that is initialized with the raw content bytes.

Overrides:
getInputStream in class DataStream
Returns:
an InputStream with the contents of this Data object

toASN1Object

public ASN1Object toASN1Object()
                        throws CMSException
Returns this CMS Data as ASN1Object. From the internal value an ASN.1 OCTET STRING object is created.

Specified by:
toASN1Object in interface ContentStream
Overrides:
toASN1Object in class DataStream
Returns:
this Data as ASN1Object (OCTET_STRING)
Throws:
CMSException - if an ASN.1 parsing error occurs

getEncoded

public byte[] getEncoded()
                  throws CMSException
Returns the DER encoding of this Data object as byte array. In general, the data is returned as primitive definte encoded octet string:
 0x04 <length> <data>
 
However, if a positive blocksize has been specified when creating this Data object, the encoding will be indefinite constructed:
 0x24 0x80
           0x04 <blocksize> <data>
           0x04 <blocksize> <data>
           0x04 <blocksize> <data>
                ...
 0x00 0x00
 

Returns:
the DER encoding of this Data
Throws:
CMSException - if an encoding error occurs

toString

public java.lang.String toString(boolean detailed)
Returns a string giving some - if requested - detailed information about this Data object.

Specified by:
toString in interface ContentStream
Overrides:
toString in class DataStream
Parameters:
detailed - - whether or not to give detailed information
Returns:
the string representation

This Javadoc may contain text parts from text parts from IETF Internet Standard specifications (see copyright note).

IAIK-CMS 6.0, (c) 2002 IAIK, (c) 2003, 2023 SIC