Class CMSAuthenticatedDataStreamGenerator


public class CMSAuthenticatedDataStreamGenerator extends CMSAuthenticatedGenerator
General class for generating a CMS authenticated-data message stream.

A simple example of usage.

     CMSAuthenticatedDataStreamGenerator edGen = new CMSAuthenticatedDataStreamGenerator();

     edGen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(cert).setProvider("BC"));

     ByteArrayOutputStream  bOut = new ByteArrayOutputStream();

     OutputStream out = edGen.open(
                             bOut, new JceCMSMacCalculatorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider("BC").build());*
     out.write(data);

     out.close();

Stream handling note:

  • The returned OutputStream must be closed to finalize the CMS structure and emit the MAC.
  • Closing the returned stream does not close the underlying OutputStream passed to open().
  • Callers are responsible for closing the underlying OutputStream separately.
  • Constructor Details

    • CMSAuthenticatedDataStreamGenerator

      public CMSAuthenticatedDataStreamGenerator()
      base constructor
  • Method Details

    • setBufferSize

      public void setBufferSize(int bufferSize)
      Set the underlying string size for encapsulated data
      Parameters:
      bufferSize - length of octet strings to buffer the data.
    • setBEREncodeRecipients

      public void setBEREncodeRecipients(boolean useBerEncodingForRecipients)
      Use a BER Set to store the recipient information. By default recipients are stored in a DER encoding.
      Parameters:
      useBerEncodingForRecipients - true if a BER set should be used, false if DER.
    • open

      public OutputStream open(OutputStream out, MacCalculator macCalculator) throws CMSException
      generate an authenticated data structure with the encapsulated bytes marked as DATA.
      Parameters:
      out - the stream to store the authenticated structure in.
      macCalculator - calculator for the MAC to be attached to the data.
      Throws:
      CMSException
    • open

      public OutputStream open(OutputStream out, MacCalculator macCalculator, DigestCalculator digestCalculator) throws CMSException
      Throws:
      CMSException
    • open

      public OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier dataType, OutputStream out, MacCalculator macCalculator) throws CMSException
      generate an authenticated data structure with the encapsulated bytes marked as type dataType.
      Parameters:
      dataType - the type of the data been written to the object.
      out - the stream to store the authenticated structure in.
      macCalculator - calculator for the MAC to be attached to the data.
      Throws:
      CMSException
    • open

      public OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier dataType, OutputStream out, MacCalculator macCalculator, DigestCalculator digestCalculator) throws CMSException
      generate an authenticated data structure with the encapsulated bytes marked as type dataType.
      Parameters:
      dataType - the type of the data been written to the object.
      out - the stream to store the authenticated structure in.
      macCalculator - calculator for the MAC to be attached to the data.
      digestCalculator - calculator for computing digest of the encapsulated data.
      Throws:
      CMSException
    • open

      public OutputStream open(OutputStream out, long inputLength, MacCalculator macCalculator) throws CMSException, IOException
      Generate an authenticated data structure for inputLength content octets, with the encapsulated bytes marked as DATA. In definite-length mode (see CMSEnvelopedGenerator.setEncoding(String)) the length is used to pre-compute every enclosing header, so exactly that many content octets must then be written to the returned stream - a mismatch fails with an IOException, by which point the output is unusable and must be discarded. In BER mode the length is ignored.

      In definite-length mode the buffer size and BER recipient set settings are ignored (an indefinite-length encoding may not appear inside a definite-length one), and the MAC algorithm must have a spec-defined output length (the HMAC family) so the mac field can be sized before the content streams. Nothing is buffered, so content larger than a Java array can carry is supported.

      Parameters:
      out - the stream to store the authenticated structure in.
      inputLength - the number of content octets that will be written.
      macCalculator - calculator for the MAC to be attached to the data.
      Throws:
      CMSException
      IOException
    • open

      public OutputStream open(OutputStream out, long inputLength, MacCalculator macCalculator, DigestCalculator digestCalculator) throws CMSException, IOException
      Generate an authenticated data structure with authenticated attributes for inputLength content octets, with the encapsulated bytes marked as DATA - see open(OutputStream, long, MacCalculator).

      In definite-length mode the authenticated (and any unauthenticated) attribute sets are sized up front using a placeholder digest of the digest algorithm's output length; attribute generators must produce sets whose encoded length does not depend on the digest value, or the enclosing length enforcement fails at close time.

      Parameters:
      out - the stream to store the authenticated structure in.
      inputLength - the number of content octets that will be written.
      macCalculator - calculator for the MAC to be attached to the data.
      digestCalculator - calculator for computing digest of the encapsulated data.
      Throws:
      CMSException
      IOException
    • open

      public OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier dataType, OutputStream out, long inputLength, MacCalculator macCalculator) throws CMSException, IOException
      Generate an authenticated data structure for inputLength content octets, with the encapsulated bytes marked as type dataType - see open(OutputStream, long, MacCalculator).
      Parameters:
      dataType - the type of the data been written to the object.
      out - the stream to store the authenticated structure in.
      inputLength - the number of content octets that will be written.
      macCalculator - calculator for the MAC to be attached to the data.
      Throws:
      CMSException
      IOException
    • open

      public OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier dataType, OutputStream out, long inputLength, MacCalculator macCalculator, DigestCalculator digestCalculator) throws CMSException, IOException
      Generate an authenticated data structure for inputLength content octets, with the encapsulated bytes marked as type dataType - see open(OutputStream, long, MacCalculator) and open(OutputStream, long, MacCalculator, DigestCalculator).
      Parameters:
      dataType - the type of the data been written to the object.
      out - the stream to store the authenticated structure in.
      inputLength - the number of content octets that will be written.
      macCalculator - calculator for the MAC to be attached to the data.
      digestCalculator - calculator for computing digest of the encapsulated data.
      Throws:
      CMSException
      IOException