public class CMSSignedDataStreamGenerator extends CMSSignedGenerator
A simple example of usage.
X509Certificate signCert = ...
certList.add(signCert);
Store certs = new JcaCertStore(certList);
ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider("BC").build(signKP.getPrivate());
CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();
gen.addSignerInfoGenerator(
new JcaSignerInfoGeneratorBuilder(
new JcaDigestCalculatorProviderBuilder().setProvider("BC").build())
.build(sha1Signer, signCert));
gen.addCertificates(certs);
OutputStream sigOut = gen.open(bOut);
sigOut.write("Hello World!".getBytes());
sigOut.close();
Stream handling note:
open()._signers, certs, crls, DATA, DIGEST_GOST3411, DIGEST_MD5, DIGEST_RIPEMD128, DIGEST_RIPEMD160, DIGEST_RIPEMD256, DIGEST_SHA1, DIGEST_SHA224, DIGEST_SHA256, DIGEST_SHA384, DIGEST_SHA512, digestAlgIdFinder, digests, encoding, ENCRYPTION_DSA, ENCRYPTION_ECDSA, ENCRYPTION_ECGOST3410, ENCRYPTION_ECGOST3410_2012_256, ENCRYPTION_ECGOST3410_2012_512, ENCRYPTION_GOST3410, ENCRYPTION_RSA, ENCRYPTION_RSA_PSS, extraDigestAlgorithms, signerGens| Constructor and Description |
|---|
CMSSignedDataStreamGenerator()
base constructor
|
CMSSignedDataStreamGenerator(DigestAlgorithmIdentifierFinder digestAlgIdFinder)
base constructor with a custom DigestAlgorithmIdentifierFinder
|
| Modifier and Type | Method and Description |
|---|---|
void |
generate(CMSTypedData content,
java.io.OutputStream out)
Write a definite-length (DL or DER, per
CMSSignedGenerator.setEncoding(String))
signed object with encapsulated content in two passes over the supplied
content, with nothing buffered - so the content may exceed the size of a
Java array, and no length needs to be known in advance. |
java.util.List<org.bouncycastle.asn1.x509.AlgorithmIdentifier> |
getDigestAlgorithms()
Return a list of the current Digest AlgorithmIdentifiers applying to the next signature.
|
java.io.OutputStream |
open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
boolean encapsulate)
generate a signed object that for a CMS Signed Data
object using the given provider - if encapsulate is true a copy
of the message will be included in the signature.
|
java.io.OutputStream |
open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
boolean encapsulate,
java.io.OutputStream dataOutputStream)
Open an OutputStream that in closing will generate a signed object
for a CMS Signed Data object - if encapsulate is true a copy
of the message will be included in the signature.
|
java.io.OutputStream |
open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
long contentLength)
Generate a definite-length signed object with encapsulated content of
exactly
contentLength octets. |
java.io.OutputStream |
open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
long contentLength,
java.io.OutputStream dataOutputStream)
Generate a definite-length (DL or DER, per
CMSSignedGenerator.setEncoding(String))
signed object with encapsulated content of exactly contentLength
octets, in a single pass with nothing buffered - so the content may
exceed the size of a Java array. |
java.io.OutputStream |
open(java.io.OutputStream out)
generate a signed object that for a CMS Signed Data
object using the given provider.
|
java.io.OutputStream |
open(java.io.OutputStream out,
boolean encapsulate)
generate a signed object that for a CMS Signed Data
object using the given provider - if encapsulate is true a copy
of the message will be included in the signature with the
default content type "data".
|
java.io.OutputStream |
open(java.io.OutputStream out,
boolean encapsulate,
java.io.OutputStream dataOutputStream)
generate a signed object that for a CMS Signed Data
object using the given provider - if encapsulate is true a copy
of the message will be included in the signature with the
default content type "data".
|
java.io.OutputStream |
open(java.io.OutputStream out,
long contentLength)
Generate a definite-length signed object with encapsulated content of
exactly
contentLength octets, the content type marked as DATA. |
void |
setBufferSize(int bufferSize)
Set the underlying string size for encapsulated data
|
addAttributeCertificate, addAttributeCertificates, addCertificate, addCertificates, addCRL, addCRLs, addDigestAlgorithms, addOtherRevocationInfo, addOtherRevocationInfo, addSignerInfoGenerator, addSigners, getBaseParameters, getGeneratedDigests, setEncodingpublic CMSSignedDataStreamGenerator()
public CMSSignedDataStreamGenerator(DigestAlgorithmIdentifierFinder digestAlgIdFinder)
public void setBufferSize(int bufferSize)
bufferSize - length of octet strings to buffer the data.public java.io.OutputStream open(java.io.OutputStream out)
throws java.io.IOException
java.io.IOExceptionpublic java.io.OutputStream open(java.io.OutputStream out,
boolean encapsulate)
throws java.io.IOException
java.io.IOExceptionpublic java.io.OutputStream open(java.io.OutputStream out,
boolean encapsulate,
java.io.OutputStream dataOutputStream)
throws java.io.IOException
out - stream the CMS object is to be written to.encapsulate - true if data should be encapsulated.dataOutputStream - output stream to copy the data being signed to.java.io.IOExceptionpublic java.io.OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
boolean encapsulate)
throws java.io.IOException
java.io.IOExceptionpublic java.io.OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
boolean encapsulate,
java.io.OutputStream dataOutputStream)
throws java.io.IOException
eContentType - OID for data to be signed.out - stream the CMS object is to be written to.encapsulate - true if data should be encapsulated.dataOutputStream - output stream to copy the data being signed to.java.io.IOExceptionpublic java.io.OutputStream open(java.io.OutputStream out,
long contentLength)
throws CMSException,
java.io.IOException
contentLength octets, the content type marked as DATA.
See open(ASN1ObjectIdentifier, OutputStream, long, OutputStream).CMSExceptionjava.io.IOExceptionpublic java.io.OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
long contentLength)
throws CMSException,
java.io.IOException
contentLength octets.
See open(ASN1ObjectIdentifier, OutputStream, long, OutputStream).CMSExceptionjava.io.IOExceptionpublic java.io.OutputStream open(org.bouncycastle.asn1.ASN1ObjectIdentifier eContentType,
java.io.OutputStream out,
long contentLength,
java.io.OutputStream dataOutputStream)
throws CMSException,
java.io.IOException
CMSSignedGenerator.setEncoding(String))
signed object with encapsulated content of exactly contentLength
octets, in a single pass with nothing buffered - so the content may
exceed the size of a Java array.
The SignerInfos trail the content in the encoding but their length feeds
the enclosing headers, which are written before any content flows. Every
SignerInfoGenerator must therefore be able to pre-commit its
encoded SignerInfo length (see
SignerInfoGenerator.getPredictedEncodedLength(org.bouncycastle.asn1.ASN1ObjectIdentifier)): the underlying
signer has to implement
FixedLengthContentSigner - RSA,
Ed25519/Ed448 and ML-DSA qualify; DER-encoded ECDSA/DSA do not - and any
attribute generators must be length-stable. Exactly contentLength
octets must then be written to the returned stream; any mismatch,
including a SignerInfo coming out at other than its predicted length,
fails with an IOException, by which point the output is unusable and
must be discarded.
eContentType - the type of the data being written to the object.out - stream the CMS object is to be written to.contentLength - the exact number of content octets that will be written.dataOutputStream - output stream to copy the content to as it is processed (may be null).CMSExceptionjava.io.IOExceptionpublic void generate(CMSTypedData content, java.io.OutputStream out) throws CMSException, java.io.IOException
CMSSignedGenerator.setEncoding(String))
signed object with encapsulated content in two passes over the supplied
content, with nothing buffered - so the content may exceed the size of a
Java array, and no length needs to be known in advance.
Pass one streams the content through the signers' digest calculators
and computes every signature, so all lengths are exact - unlike the
single-pass open(OutputStream, long) this works with
variable-length signature algorithms such as DER-encoded ECDSA. Pass two
writes the structure, re-reading the content from content -
which must therefore be re-readable (e.g. file-backed) and stable: the
second pass is re-digested and compared against the first, so a source
that changed between passes fails with an IOException rather than
producing a structure whose signatures don't verify.
content - the content to sign and encapsulate; write is invoked twice.out - stream the CMS object is to be written to.CMSExceptionjava.io.IOExceptionpublic java.util.List<org.bouncycastle.asn1.x509.AlgorithmIdentifier> getDigestAlgorithms()