iaik.pkcs.pkcs7
Class Data

java.lang.Object
  |
  +--iaik.pkcs.pkcs7.DataStream
        |
        +--iaik.pkcs.pkcs7.Data
All Implemented Interfaces:
Content, ContentStream

public class Data
extends DataStream
implements Content

This class represents the non-stream implementation of the PKCS#7 content type Data.

Each PKCS#7 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.1.113549.1.7.1".

PKCS#7 specifies the Data content type as base type without any cryptographic enhancements:

Data ::= OCTET STRING

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 - for instance some versions of Netscape Navigator - 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 PKCS#7 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.

Version:
File Revision 30
See Also:
Content, ContentInfo, OCTET_STRING

Constructor Summary
protected Data()
          Default constructor for dynamic object creation in ContentInfo.
  Data(ASN1Object obj)
          Creates a PKCS#7 Data object from an ASN1Object.
  Data(byte[] content)
          Creates a PKCS#7 Data object from a byte array supplying the data value.
  Data(byte[] content, int blockSize)
          Creates a PKCS#7 Data object from a byte array supplying the data value and a blockSize specifying the encoding scheme.
  Data(InputStream is)
          Creates a new PKCS#7 Data from a DER encoded InputStream.
 
Method Summary
 void decode(ASN1Object obj)
          Reads and decodes the Data from an ASN1Object.
 void decode(InputStream is)
          Reads and decodes the Data from an InputStream.
 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.
 InputStream getInputStream()
          Returns an InputStream from which the contents of this object can be read.
 ASN1Object toASN1Object()
          Returns this PKCS#7 Data as ASN1Object.
 String toString(boolean detailed)
          Returns a string giving some - if requested - detailed information about this Data object.
 
Methods inherited from class iaik.pkcs.pkcs7.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.pkcs.pkcs7.ContentStream
getBlockSize, getContentType, setBlockSize
 

Constructor Detail

Data

protected Data()
Default constructor for dynamic object creation in ContentInfo. The block size is set to -1 meaning that the data will be encoded a one simple octet string by deafult.

Data

public Data(byte[] content)
Creates a PKCS#7 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 PKCS#7 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 PKCSParsingException
Creates a PKCS#7 Data object from an ASN1Object. 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 PKCS#7 Data as ASN1Object
Throws:
PKCSParsingException - if an parsing error occurs

Data

public Data(InputStream is)
     throws IOException,
            PKCSParsingException
Creates a new PKCS#7 Data from a DER encoded InputStream. 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:
IOException - if an I/O error occurs during reading from the InputStream
PKCSParsingException - if an error occurs while parsing the object
Method Detail

decode

public void decode(ASN1Object obj)
            throws PKCSParsingException
Reads and decodes the Data from an ASN1Object.
Specified by:
decode in interface Content
Parameters:
obj - the PKCS#7 Data object as ASN1Object
Throws:
PKCSParsingException - if an error occurs while parsing the object

decode

public void decode(InputStream is)
            throws IOException,
                   PKCSParsingException
Reads and decodes the Data from an InputStream.

Specified by:
decode in interface ContentStream
Overrides:
decode in class DataStream
Parameters:
is - the InputStream holding a DER encoded PKCS#7 Data object
Throws:
IOException - if an I/O error occurs during reading from the InputStream
PKCSParsingException - 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 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 PKCSException
Returns this PKCS#7 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:
PKCSException - if an ASN.1 parsing error occurs
See Also:
OCTET_STRING

getEncoded

public byte[] getEncoded()
                  throws PKCSException
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:
PKCSException - if an encoding error occurs

toString

public 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 Internet Standard specifications (RFC 2459, 3280, 3039, 2560, 1521, 821, 822, 2253, 1319, 1321, ,2630, 2631, 2268, 3058, 2984, 2104, 2144, 2040, 2311, 2279, see copyright note) and RSA Data Security Public-Key Cryptography Standards (PKCS#1,3,5,7,8,9,10,12, see copyright note).

IAIK-JCE 3.1 with IAIK-JCE CC Core 3.1, (c) 1997-2004 IAIK