iaik.cms
Class ContentInfo

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

public class ContentInfo
extends java.lang.Object

This class represents the non-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 non-stream implementations of the several CMS content types. And the second non-static part provides constructors and methods for creating, en- and decoding non-stream CMS content type objects in the usual way.

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

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

 // create a CMS object, e.g. SignedData:
 SignedData signedData = ...;
 ...
 // create a ContentInfo for the CMS object and encode it to a stream:
 ContentInfo ci = new ContentInfo(signedData);
 OutputStream encoded_stream = ...;
 ci.writeTo(encoded_stream);
 
After creating an ContentInfo instance for parsing an already existing ContentInfo object supplied as ASN.1 object or BER encoded from an input stream, you may use the getContentInputStream method for getting the unparsed content as input stream, or you may call the getContent method for getting the already parsed content as Content descendant.

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 ContentInfo from the encoding:
 ContentInfo ci = new ContentInfo(encodedStream);
 // ask for the content type:
 if (ci.getContentType().equals(ObjectID.cms_signedData)) {
    // get the SignedData content:
    SignedData signedData = (SignedData)ci.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 SignedData(byte[] content, AlgorithmID[] hashAlgorithms) constructor for initializing a SignedData 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 by a byte array:
 byte[] message = ...;
 // 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 SignedData object from raw data and hash algorithms:
 SignedData signed_data = new SignedData(message, algIDs);
 // create a ContentInfo from the BER encoding:
 ContentInfo ci = new ContentInfo(encoded_stream);
 // check the content type:
 if (ci.getContentType().equals(ObjectID.cms_signedData)) {
    // now decode the BER encoded SignedData obtained from the ContentInfo:
    signed_data.decode(ci.getContentInputStream());
 } else {
    throw new CMSParsingException("Error! Expected a SignedData content!");
 }
 // proceed as usual for reading the content, getting SignerInfos and verifying signatures
 ...
 

See Also:
Data, DigestedData, EncryptedData, EnvelopedData, SignedData, AuthenticatedData, CompressedData, Content, Receipt

Constructor Summary
ContentInfo(ASN1Object obj)
          Creates a CMS ContentInfo from an ASN1Object.
ContentInfo(Content content)
          Creates a CMS ContentInfo from the given content value.
ContentInfo(java.io.InputStream is)
          Creates a new ContentInfo where the BER encoded data is read from the given InputStream.
 
Method Summary
static Content create(ObjectID oid)
          Returns an instance of the specified CMS content type implementation, defined by its ASN.1 ObjectID.
static Content create(ObjectID oid, ASN1Object obj)
          Returns an instance of the specified CMS content type implementation, defined by its ASN.1 ObjectID.
protected  void decode(DerInputStream is)
          Reads and decodes a BER encoded ContentInfo from the given input stream.
 Content getContent()
          Returns the content of this CMS ContentInfo.
 java.io.InputStream getContentInputStream()
          Returns the unparsed content of this CMS ContentInfo as BER encoding.
 ObjectID getContentType()
          Returns the content type of this CMS ContentInfo.
 byte[] getEncoded()
          Returns this CMS ContentInfo as BER encoded byte array.
static void keepEncodedContent(boolean keepEncodedContent)
          Decides whether to keep the encoded content during parsing to can be repeatedly accessed when calling method getContentInputStream.
static void register(ObjectID oid, java.lang.Class cls)
          Registers a new implementation for a Content defined through the given ObjectID.
 void setContent(Content content)
          Sets the content of this ContentInfo from the given content value.
 ASN1Object toASN1Object()
          Returns this CMS ContentInfo as ASN1Object.
 byte[] toByteArray()
          Returns this CMS ContentInfo as BER encoded byte array.
 java.lang.String toString()
          Returns a string giving some information about this CMS ContentInfo.
 java.lang.String toString(boolean detailed)
          Returns a string giving some - if requested - detailed information about this CMS ContentInfo.
 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

ContentInfo

public ContentInfo(Content content)
Creates a CMS ContentInfo from the given content value.

Parameters:
content - the content object as Content descendant.

ContentInfo

public ContentInfo(ASN1Object obj)
            throws CMSParsingException
Creates a CMS ContentInfo from an ASN1Object. The given ASN1Object represents an alredy existing ContentInfo object which may have been created by using the toASN1Object method of this class.

Parameters:
obj - the CMS ContentInfo as an ASN1Object
Throws:
CMSParsingException - if the ASN1Object could not be parsed

ContentInfo

public ContentInfo(java.io.InputStream is)
            throws java.io.IOException,
                   CMSParsingException
Creates a new ContentInfo where the BER encoded data is read from the given InputStream. The given input stream represents the BER encoding of an alredy existing ContentInfo object which may have been written to a stream by using the writeTo method of this class.

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 Content create(ObjectID oid)
                      throws CMSException
Returns an 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 CMS content type
Returns:
the implementation of the content with this oid
Throws:
CMSException - if an error occurs when creating the CMS content object

create

public static Content create(ObjectID oid,
                             ASN1Object obj)
                      throws CMSParsingException
Returns an instance of the specified CMS content type implementation, defined by its ASN.1 ObjectID. The new instance will be initialized with the given ASN1Object. This method belongs to the static part of this class.

This method only calls the corresponding create method of the ExtensionFactory for obtaining a non-stream supporting implementation of the desired CMS content type implementation, specified by its object identifier.

Parameters:
oid - the ObjectID of the CMS content type
obj - an ASN1Object representing the CMS content object specified by the given oid
Returns:
the initialized implementation of the CMS content object identified by the given oid
Throws:
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 Content defined through the given ObjectID. This method registeres the given class as non-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 to be registered
cls - the implementing class

keepEncodedContent

public static void keepEncodedContent(boolean keepEncodedContent)
Decides whether to keep the encoded content during parsing to can be repeatedly accessed when calling method getContentInputStream.

The default value is false meaning that the encoding of the inherent CMS (SignedData, EnvelopedData, ...) objcet is not kept. Thus you should call method getContentInputStream only once. Use this method and set keepEncodedContent to true if you want to change the default behaviour and keep the encoded content.
However, generally it should not necessary to call this method at all since it should not be required to keep the encoded CMS content. Usually you will create a CMS content object by immediately using the corresponding content type implementation (e.g. SignedData, EnvelopedData). For the non-stream based implementation method getContentInputStream may not be used at all.

Parameters:
keepEncodedContent - whether to keep the encoded content object or not (default: false)

decode

protected void decode(DerInputStream is)
               throws java.io.IOException,
                      CMSParsingException
Reads and decodes a BER encoded ContentInfo from the given input stream.

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

toASN1Object

public ASN1Object toASN1Object()
                        throws CMSException
Returns this CMS ContentInfo as ASN1Object. The ASN1Object returned by this method may be used as parameter value when creating a CMS ContentInfo from an ASN1Object by calling the ContentInfo(ASN1Object obj) constructor.

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

toByteArray

public byte[] toByteArray()
                   throws CMSException
Returns this CMS ContentInfo as BER encoded byte array.

Returns:
this CMS ContentInfo as BER encoded byte array
Throws:
CMSException - if an error occurs while encoding the object

getEncoded

public byte[] getEncoded()
                  throws CMSException
Returns this CMS ContentInfo as BER encoded byte array.

Returns:
this CMS ContentInfo as BER encoded byte array
Throws:
CMSException - if an error occurs while encoding the object

writeTo

public void writeTo(java.io.OutputStream os)
             throws java.io.IOException,
                    CMSException
Writes the BER encoding of this object to the given OutputStream.

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 Content getContent()
                   throws CMSRuntimeException
Returns the content of this CMS ContentInfo.

Returns:
the content value as Content descendant or null if there is no content
Throws:
CMSRuntimeException - if the content has yet not been parsed and an error occurs when parsing it now

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 SignedData(byte[] content, AlgorithmID[] hashAlgorithms) in the normal way for initializing a SignedData 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 as byte array:
 byte[] message = ...;
 // 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 SignedData object from raw data and hash algorithms:
 SignedData signed_data = new SignedData(message, algIDs);
 // create a ContentInfo from the BER encoding:
 ContentInfo ci = new ContentInfo(der_stream);
 // check the content type:
 if (ci.getContentType().equals(ObjectID.cms_signedData)) {
    // now decode the BER encoded SignedData obtained from the ContentInfo:
    signed_data.decode(ci.getContentInputStream());
 } else {
    throw new CMSParsingException("Error! Expected a SignedData content!");
 }
 // proceed as usual for getting SignerInfos and verifying signatures
 ...
 

Returns:
an input stream carrying the BER encoding of the inherent content or null if there is no content or this method is not called during the parsing (decoding) process

getContentType

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

Returns:
the content type, as ObjectID

setContent

public void setContent(Content content)
Sets the content of this ContentInfo from the given content value.

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

Parameters:
content - the content object as Content descendant.

toString

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

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 ContentInfo.

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