Class CMSAuthenticatedDataParser

java.lang.Object
org.bouncycastle.cms.CMSContentInfoParser
org.bouncycastle.cms.CMSAuthenticatedDataParser

public class CMSAuthenticatedDataParser extends CMSContentInfoParser
Parsing class for an CMS Authenticated Data object from an input stream.

Note: that because we are in a streaming mode only one recipient can be tried and it is important that the methods on the parser are called in the appropriate order.

Example of use - assuming the first recipient matches the private key we have.

     CMSAuthenticatedDataParser     ad = new CMSAuthenticatedDataParser(inputStream);

     RecipientInformationStore  recipients = ad.getRecipientInfos();

     Collection  c = recipients.getRecipients();
     Iterator    it = c.iterator();

     if (it.hasNext())
     {
         RecipientInformation   recipient = (RecipientInformation)it.next();

         CMSTypedStream recData = recipient.getContentStream(new JceKeyTransAuthenticatedRecipient(privateKey).setProvider("BC"));

         processDataStream(recData.getContentStream());

         if (!Arrays.constantTimeAreEqual(ad.getMac(), recipient.getMac()))
         {
             System.err.println("Data corrupted!!!!");
         }
     }
 
Note: this class does not introduce buffering - if you are processing large files you should create the parser with:
         CMSAuthenticatedDataParser     ep = new CMSAuthenticatedDataParser(new BufferedInputStream(inputStream, bufSize));
 
where bufSize is a suitably large buffer size.

Stream handling note:

  • The constructor reads only enough of the supplied InputStream to expose the CMS structure metadata (originator info, recipient infos, MAC algorithm). The encapsulated content is drained lazily by the caller via RecipientInformation.getContentStream(Recipient); the MAC is available from getMac() once the content stream has been read to EOF.
  • The supplied InputStream is not closed automatically. Call CMSContentInfoParser.close() on this parser (inherited from CMSContentInfoParser) to close the underlying InputStream, or close it yourself.
  • Constructor Details

  • Method Details

    • getOriginatorInfo

      public OriginatorInformation getOriginatorInfo()
      Return the originator information associated with this message if present.
      Returns:
      OriginatorInformation, null if not present.
    • getMacAlgorithm

      public org.bouncycastle.asn1.x509.AlgorithmIdentifier getMacAlgorithm()
      Return the MAC algorithm details for the MAC associated with the data in this object.
      Returns:
      AlgorithmIdentifier representing the MAC algorithm.
    • getMacAlgOID

      public String getMacAlgOID()
      return the object identifier for the mac algorithm.
    • getMacAlgParams

      public byte[] getMacAlgParams()
      return the ASN.1 encoded encryption algorithm parameters, or null if there aren't any.
    • getRecipientInfos

      public RecipientInformationStore getRecipientInfos()
      return a store of the intended recipients for this message
    • getMac

      public byte[] getMac() throws IOException
      Throws:
      IOException
    • getAuthAttrs

      public org.bouncycastle.asn1.cms.AttributeTable getAuthAttrs() throws IOException
      return a table of the unauthenticated attributes indexed by the OID of the attribute.
      Throws:
      IOException
    • getUnauthAttrs

      public org.bouncycastle.asn1.cms.AttributeTable getUnauthAttrs() throws IOException
      return a table of the unauthenticated attributes indexed by the OID of the attribute.
      Throws:
      IOException
    • getContentDigest

      public byte[] getContentDigest()
      This will only be valid after the content has been read.
      Returns:
      the contents of the messageDigest attribute, if available. Null if not present.