iaik.cms
Class ContentInfoStream

java.lang.Object
  extended by iaik.cms.ContentInfoStream

public class ContentInfoStream
extends java.lang.Object

This class represents the stream implemention of the CMS ContentInfo type.

The Cryptographic Message Syntax (CMS) ( RFC 5652) describes a general syntax for data that may have cryptography applied to it, such as digital signatures and digital envelopes.

The ASN.1 type ContentInfo type associates a content type (data, signedData, envelopedData, authenticatedData, digestedData, encryptedData) with some particular content for specifying a general syntax for content exchanged between entities according the CMS standard:

 ContentInfo ::= SEQUENCE {
    contentType ContentType,
    content
      [0] EXPLICIT ANY DEFINED BY contentType }
 
ContentType ::= OBJECT IDENTIFIER

This class consists of two parts: the first static part implements a factory for registering all the stream implementations of the several CMS content types. And the second non-static part provides constructors and methods for creating, en- and decoding stream CMS content type objects in the usual way.

The non-stream supporting equivalent to this class is implemented by the ContentInfo class.

When creating a new ContentInfo to be sent use the ContentInfoStream(ContentStream) constructor and subsequently call the writeTo method, e.g.:

 // create a CMS object, e.g. SignedData:
 SignedDataStream signedData = ...;
 ...
 // create a ContentInfo for the CMS object and encode it to a stream:
 ContentInfoStream cis = new ContentInfoStream(signedData);
 OutputStream encoded_stream = ...;
 cis.writeTo(encoded_stream);
 
For parsing an already existing BER encoded ContentInfo object supplied from an input stream, use the ContentInfoStream(Inputstream) constructor. This will invoke the decode method for starting the decoding procedure. The decode method only parses the contentType object identifier and lets the content itself unparsed. This provides the possibility for getting the unparsed content by means of the getContentInputStream method. This will give you the possibility of doing the decoding and parsing by yourself. Alternatively you may call the getContent method for getting the content as ContentStream descendant. Actually when calling getContent the encoded content will be decoded and parsed for its internal structure. However, it is essential to remember that this is a stream implementation, so using both getContent and getContentInputStream would not work!

The different usage may be illustrated by means of the two types of a SignedData message. When receiving a ContentInfo holding an implicit message the content is included in the SignedData object, and so the getContent method may be appropriate for getting the already parsed SignedData content, e.g.:

 // create a ContentInfoStream from the encoding:
 ContentInfoStream cis = new ContentInfoStream(encodedStream);
 // ask for the content type:
 if (cis.getContentType().equals(ObjectID.cms_signedData)) {
    // get the SignedData content:
    SignedDataStream signedData = (SignedDataStream)cis.getContent()
    // proceed as usual for reading the data, getting the SignerInfos and 
    // verifying the signatures
 } else {
    throw new CMSParsingException("Error! Expected content type SignedData!");
 }
 
However, when receiving a ContentInfo holding an explicit message where the content is not included in the SignedData object, you may want to use the the SignedDataStream(InputStream data_is, AlgorithmID[] hashAlgorithms) constructor for initializing a SignedDataStream object with raw content and hash algorithms in the usual way, and subsequently decode the received SignedData object by feeding the decode method with the BER encoded SignedData object obtained from the ContentInfo by calling the getContentInputStream method, e.g.:
 // the raw data received by other means, supplied from an input stream:
 InputStream data_is = ...;
 // the hash algorithms (e.g. parsed from the headers of a S/MIME multipart/signed entity):
 AlgorithmID[] algIDs = { AlgorithmID.sha1 };
 // the BER encoded content info, supplied from an input stream:
 InputStream encoded_stream = ...;
 // create a SignedDataStream object from raw data and hash algorithms:
 SignedDataStream signed_data = new SignedDataStream(data_is, algIDs);
 // create a ContentInfoStream from the BER encoding:
 ContentInfoStream cis = new ContentInfoStream(encoded_stream);
 // check the content type:
 if (cis.getContentType().equals(ObjectID.cms_signedData)) {
    // now decode the BER encoded SignedData obtained from the ContentInfo:
    signed_data.decode(cis.getContentInputStream());
 } else {
    throw new CMSParsingException("Error! Expected a SignedData content!");
 }
 // proceed as usual for reading the content, getting SignerInfos and verifying signatures
 ...
 
Attention! After using the getContentInputStream method for getting the unparsed content an application might not further access the corresponding ContentInfoStream object. When getting and reading the unparsed content in the way described above, the stream holding the BER encoded content will be read outside this class for decoding and parsing the inherent content. It is quite obviously that in this case the content component of the corresponding ContentInfoStream object will remain empty, unless you explicitly set it again by using the setContent(ContentStream content) method.

See Also:
DataStream, DigestedDataStream, EncryptedDataStream, EnvelopedDataStream, SignedDataStream, AuthenticatedDataStream, CompressedDataStream, ContentStream

Constructor Summary
ContentInfoStream(ContentStream content)
          Creates a CMS ContentInfoStream from the given content value.
ContentInfoStream(java.io.InputStream is)
          Creates a new ContentInfoStream where the BER encoded data is read from the given InputStream.
 
Method Summary
static ContentStream create(ObjectID oid)
          Returns a stream-supporting instance of the specified CMS content type implementation, defined by its ASN.1 ObjectID.
static ContentStream create(ObjectID oid, java.io.InputStream is)
          Returns a stream-supporting instance of the specified CMS content type implementation, defined by its ASN.1 ObjectID.
protected  void decode(DerInputStream is)
          Reads and decodes the ContentInfo from a DerInputStream.
 ContentStream getContent()
          Returns the content of this CMS ContentInfoStream.
 java.io.InputStream getContentInputStream()
          Returns the unparsed content of this CMS ContentInfo as BER encoding.
 ObjectID getContentType()
          Returns the content type of this CMS ContentInfoStream.
static void register(ObjectID oid, java.lang.Class cls)
          Registers a new implementation for a ContentStream defined through the given ObjectID.
 void setContent(ContentStream content)
          Sets the content of this ContentInfo.
 ASN1Object toASN1Object()
          Returns this CMS ContentInfoStream as ASN1Object.
 java.lang.String toString()
          Returns a string giving some information about this CMS ContentInfoStream.
 java.lang.String toString(boolean detailed)
          Returns a string giving some - if requested - detailed information about this CMS ContentInfoStream.
 void writeTo(java.io.OutputStream os)
          Writes the BER encoding of this object to the given OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ContentInfoStream

public ContentInfoStream(ContentStream content)
Creates a CMS ContentInfoStream from the given content value.

Parameters:
content - the content object as ContentStream descendant.

ContentInfoStream

public ContentInfoStream(java.io.InputStream is)
                  throws java.io.IOException,
                         CMSParsingException
Creates a new ContentInfoStream where the BER encoded data is read from the given InputStream. Attention!This constructor calls the decode method for initializing the decoding procedure. The decode method only parses the contentType object identifier and lets the content itself unparsed. This provides the possibility for getting the unparsed content by means of the getContentInputStream method. Alternatively you may call the getContent method for getting the content as ContentStream descendant. Actually when calling getContent the encoded content will be decoded and parsed for its internal structure. However, it is essential to remember that this is a stream implementation, so using both getContent and getContentInputStream would not work!

Parameters:
is - the InputStream holding a BER encoded CMS ContentInfo 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
Method Detail

create

public static ContentStream create(ObjectID oid)
                            throws CMSException
Returns a stream-supporting instance of the specified CMS content type implementation, defined by its ASN.1 ObjectID. This method belongs to the static part of this class.

Parameters:
oid - the ObjectID of the extension.
Returns:
the stream implementation of the CMS content, identified by the given OID
Throws:
CMSException - if an error occurs when creating the CMS ContentStream object

create

public static ContentStream create(ObjectID oid,
                                   java.io.InputStream is)
                            throws java.io.IOException,
                                   CMSParsingException
Returns a stream-supporting instance of the specified CMS content type implementation, defined by its ASN.1 ObjectID. The new instance will be initialized with the given InputStream.

This method belongs to the static part of this class.

Parameters:
oid - the ObjectID of the CMS content
is - an InputStream holding a BER encoded version of the content specified through the oid
Returns:
the initialized stream implementation of the CMS content with this oid
Throws:
java.io.IOException - if an I/O error occurs during reading from the InputStream
CMSParsingException - if an error occurs while parsing the object

register

public static void register(ObjectID oid,
                            java.lang.Class cls)
Registers a new implementation for a ContentStream defined through the given ObjectID.

This method registers the given class as stream-supporting implementation of the supplied CMS content type. The content type has to be specified by its ObjectID. This method belongs to the static part of this class.

Parameters:
oid - the object id of the CMS content type to be registered
cls - the stream implementing class

decode

protected void decode(DerInputStream is)
               throws java.io.IOException,
                      CMSParsingException
Reads and decodes the ContentInfo from a DerInputStream. If the supplied InputStream actually is not an instance of DerInputStream, internally a DerInputStream is created before parsing the data. Attention! This method only parses the contentType object identifier and lets the content itself unparsed. This provides the possibility for getting the unparsed content by means of the getContentInputStream method. Alternatively you may call the getContent method for getting the content as ContentStream descendant. Actually when calling getContent the encoded content will be decoded and parsed for its internal structure. However, it is essential to remember that this is a stream implementation, so using both getContent and getContentInputStream would not work!

Parameters:
is - the InputStream holding a BER encoded CMS ContentInfoStream 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

toASN1Object

public ASN1Object toASN1Object()
                        throws CMSException
Returns this CMS ContentInfoStream as ASN1Object.

Returns:
this CMS ContentInfoStream as ASN1Object
Throws:
CMSException - if an ASN.1 parsing error occurs

writeTo

public void writeTo(java.io.OutputStream os)
             throws java.io.IOException,
                    CMSException
Writes the BER encoding of this object to the given OutputStream. The ContentInfo structure is encoded as ASN.1 SEQUENCE using the indefinite length encoding scheme:
 30 80
    ...
    ...
 00 00
 

Parameters:
os - the OutputStream where the encoding shall be written to
Throws:
java.io.IOException - if an I/O error occurs during writing to the OutputStream
CMSException - if an error occurs while encoding the object

getContent

public ContentStream getContent()
                         throws java.io.IOException,
                                CMSParsingException
Returns the content of this CMS ContentInfoStream. If called during a parsing process this method actually will decode and parse the BER encoded content. For that reason this method must throw an IOException and a CMSParsingException if some error occurs during the parsing procedure.

For getting the unparsed BER encoded content as, you may call the getContent method. This will give you the possibility of doing the decoding and parsing by yourself. However, since this is a stream implementation, remember that you can read the stream only once! For that reason you never should use both methods getContent and getContentInputStream!

Returns:
the content value as ContentStream descendant
Throws:
java.io.IOException - if some error occurs while reading from the stream supplying the BER encoded content
CMSParsingException - if an error occurs during parsing the content

getContentInputStream

public java.io.InputStream getContentInputStream()
Returns the unparsed content of this CMS ContentInfo as BER encoding. This method may be used for getting the BER encoding of the content for further processing it by the corresponding content implementing class itself. In this way, this method only shall be used as part of the parsing process; otherwise it will return null.

This method may be useful for parsing an explicit SignedData object that has been wrapped into a ContentInfo. You can use the SignedDataStream(InputStream data_is, AlgorithmID[] hashAlgorithms) in the normal way for initializing a SignedDataStream object with raw content and hash algorithms, and subsequently decode the received SignedData object by feeding the decode method with the BER encoded SignedData object obtained from the ContentInfo by calling this getContentInputStream method, e.g.:

 // the raw data received by other means, supplied from an input stream:
 InputStream data_is = ...;
 // the hash algorithms (e.g. parsed from the headers of a S/MIME multipart/signed entity):
 AlgorithmID[] algIDs = { AlgorithmID.sha1 };
 // the BER encoded content info, supplied from an input stream:
 InputStream der_stream = ...;
 // create a SignedDataStream object from raw data and hash algorithms:
 SignedDataStream signed_data = new SignedDataStream(data_is, algIDs);
 // create a ContentInfoStream from the BER encoding:
 ContentInfoStream cis = new ContentInfoStream(der_stream);
 // check the content type:
 if (cis.getContentType().equals(ObjectID.cms_signedData)) {
    // now decode the BER encoded SignedData obtained from the ContentInfo:
    signed_data.decode(cis.getContentInputStream());
 } else {
    throw new CMSParsingException("Error! Expected a SignedData content!");
 }
 // proceed as usual for reading the content, getting SignerInfos and verifying signatures
 ...
 
Attention! After using this method for getting the unparsed content an application might not further access the corresponding ContentInfoStream object. When getting and reading the unparsed content in the way described above, the stream holding the BER encoded content will be read outside this class for decoding and parsing the inherent content. It is quite obviously that in this case the content component of the corresponding ContentInfoStream object will remain empty, unless you explicitly set it again by using the setContent(ContentStream content) method.

For parsing and getting the content as ContentStream descendant, you may call the getContent method. However, since this is a stream implementation, you never should use both methods getContent and getContentInputStream!

Returns:
the content value as Content descendant

getContentType

public ObjectID getContentType()
Returns the content type of this CMS ContentInfoStream.

Returns:
the content type, as ObjectID

setContent

public void setContent(ContentStream content)
Sets the content of this ContentInfo.

The content type object identifier internally is derived from the given ContentStream object by using the getContentType method.

Parameters:
content - the content object as ContentStream descendant.

toString

public java.lang.String toString()
Returns a string giving some information about this CMS ContentInfoStream.

Overrides:
toString in class java.lang.Object
Returns:
the string representation

toString

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

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 5.1, (c) 2002 IAIK, (c) 2003 - 2010 SIC