FileDocCategorySizeDatePackage
CMSSignedDataStreamGenerator.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)30243Wed Oct 01 10:55:28 BST 2008org.bouncycastle.cms

CMSSignedDataStreamGenerator

public class CMSSignedDataStreamGenerator extends CMSSignedGenerator
General class for generating a pkcs7-signature message stream.

A simple example of usage.

CertStore certs...
CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();

gen.addSigner(privateKey, cert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");

gen.addCertificatesAndCRLs(certs);

OutputStream sigOut = gen.open(bOut);

sigOut.write("Hello World!".getBytes());

sigOut.close();

Fields Summary
private List
_signerInfs
private List
_messageDigests
private int
_bufferSize
Constructors Summary
public CMSSignedDataStreamGenerator()
base constructor

    
public CMSSignedDataStreamGenerator(SecureRandom rand)
constructor allowing specific source of randomness

param
rand instance of SecureRandom to use

        super(rand);
    
Methods Summary
public voidaddSigner(java.security.PrivateKey key, byte[] subjectKeyID, java.lang.String digestOID, java.lang.String sigProvider)
add a signer - no attributes other than the default ones will be provided here.

throws
NoSuchProviderException
throws
NoSuchAlgorithmException
throws
InvalidKeyException

        addSigner(key, subjectKeyID, digestOID, new DefaultSignedAttributeTableGenerator(), (CMSAttributeTableGenerator)null, sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, byte[] subjectKeyID, java.lang.String digestOID, java.security.Provider sigProvider)
add a signer - no attributes other than the default ones will be provided here.

throws
NoSuchAlgorithmException
throws
InvalidKeyException

       addSigner(key, subjectKeyID, digestOID, new DefaultSignedAttributeTableGenerator(), (CMSAttributeTableGenerator)null, sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, byte[] subjectKeyID, java.lang.String digestOID, org.bouncycastle.asn1.cms.AttributeTable signedAttr, org.bouncycastle.asn1.cms.AttributeTable unsignedAttr, java.lang.String sigProvider)
add a signer with extra signed/unsigned attributes.

throws
NoSuchProviderException
throws
NoSuchAlgorithmException
throws
InvalidKeyException

        addSigner(key, subjectKeyID, digestOID,
            new DefaultSignedAttributeTableGenerator(signedAttr), new SimpleAttributeTableGenerator(unsignedAttr), sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, byte[] subjectKeyID, java.lang.String digestOID, org.bouncycastle.asn1.cms.AttributeTable signedAttr, org.bouncycastle.asn1.cms.AttributeTable unsignedAttr, java.security.Provider sigProvider)
add a signer with extra signed/unsigned attributes.

throws
NoSuchAlgorithmException
throws
InvalidKeyException

        addSigner(key, subjectKeyID, digestOID,
            new DefaultSignedAttributeTableGenerator(signedAttr), new SimpleAttributeTableGenerator(unsignedAttr), sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, byte[] subjectKeyID, java.lang.String digestOID, CMSAttributeTableGenerator signedAttrGenerator, CMSAttributeTableGenerator unsignedAttrGenerator, java.security.Provider sigProvider)

        String        encOID = getEncOID(key, digestOID);
        String        digestName = CMSSignedHelper.INSTANCE.getDigestAlgName(digestOID);
        String        signatureName = digestName + "with" + CMSSignedHelper.INSTANCE.getEncryptionAlgName(encOID);
        Signature     sig = CMSSignedHelper.INSTANCE.getSignatureInstance(signatureName, sigProvider);
        MessageDigest dig = CMSSignedHelper.INSTANCE.getDigestInstance(digestName, sigProvider);

        sig.initSign(key, rand);

        _signerInfs.add(new SignerInf(key, subjectKeyID, digestOID, encOID, signedAttrGenerator, unsignedAttrGenerator, dig, sig));
        _messageDigests.add(dig);
    
public voidaddSigner(java.security.PrivateKey key, byte[] subjectKeyID, java.lang.String digestOID, CMSAttributeTableGenerator signedAttrGenerator, CMSAttributeTableGenerator unsignedAttrGenerator, java.lang.String sigProvider)

        addSigner(key, subjectKeyID, digestOID, signedAttrGenerator, unsignedAttrGenerator, CMSUtils.getProvider(sigProvider));
    
public voidaddSigner(java.security.PrivateKey key, java.security.cert.X509Certificate cert, java.lang.String digestOID, java.lang.String sigProvider)
add a signer - no attributes other than the default ones will be provided here.

throws
NoSuchProviderException
throws
NoSuchAlgorithmException
throws
InvalidKeyException

        addSigner(key, cert, digestOID, new DefaultSignedAttributeTableGenerator(), (CMSAttributeTableGenerator)null, sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, java.security.cert.X509Certificate cert, java.lang.String digestOID, java.security.Provider sigProvider)
add a signer - no attributes other than the default ones will be provided here.

throws
NoSuchAlgorithmException
throws
InvalidKeyException

       addSigner(key, cert, digestOID, new DefaultSignedAttributeTableGenerator(), (CMSAttributeTableGenerator)null, sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, java.security.cert.X509Certificate cert, java.lang.String digestOID, org.bouncycastle.asn1.cms.AttributeTable signedAttr, org.bouncycastle.asn1.cms.AttributeTable unsignedAttr, java.lang.String sigProvider)
add a signer with extra signed/unsigned attributes.

throws
NoSuchProviderException
throws
NoSuchAlgorithmException
throws
InvalidKeyException

        addSigner(key, cert, digestOID,
            new DefaultSignedAttributeTableGenerator(signedAttr), new SimpleAttributeTableGenerator(unsignedAttr), sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, java.security.cert.X509Certificate cert, java.lang.String digestOID, org.bouncycastle.asn1.cms.AttributeTable signedAttr, org.bouncycastle.asn1.cms.AttributeTable unsignedAttr, java.security.Provider sigProvider)
add a signer with extra signed/unsigned attributes.

throws
NoSuchAlgorithmException
throws
InvalidKeyException

        addSigner(key, cert, digestOID,
            new DefaultSignedAttributeTableGenerator(signedAttr), new SimpleAttributeTableGenerator(unsignedAttr), sigProvider);
    
public voidaddSigner(java.security.PrivateKey key, java.security.cert.X509Certificate cert, java.lang.String digestOID, CMSAttributeTableGenerator signedAttrGenerator, CMSAttributeTableGenerator unsignedAttrGenerator, java.security.Provider sigProvider)

        String        encOID = getEncOID(key, digestOID);
        String        digestName = CMSSignedHelper.INSTANCE.getDigestAlgName(digestOID);
        String        signatureName = digestName + "with" + CMSSignedHelper.INSTANCE.getEncryptionAlgName(encOID);
        Signature     sig = CMSSignedHelper.INSTANCE.getSignatureInstance(signatureName, sigProvider);
        MessageDigest dig = CMSSignedHelper.INSTANCE.getDigestInstance(digestName, sigProvider);

        sig.initSign(key, rand);

        _signerInfs.add(new SignerInf(key, cert, digestOID, encOID, signedAttrGenerator, unsignedAttrGenerator, dig, sig));
        _messageDigests.add(dig);
    
public voidaddSigner(java.security.PrivateKey key, java.security.cert.X509Certificate cert, java.lang.String digestOID, CMSAttributeTableGenerator signedAttrGenerator, CMSAttributeTableGenerator unsignedAttrGenerator, java.lang.String sigProvider)

        addSigner(key, cert, digestOID, signedAttrGenerator, unsignedAttrGenerator, CMSUtils.getProvider(sigProvider));
    
private org.bouncycastle.asn1.DERIntegercalculateVersion(java.lang.String contentOid)

        boolean otherCert = false;
        boolean otherCrl = false;
        boolean attrCertV1Found = false;
        boolean attrCertV2Found = false;

        if (_certs != null)
        {
            for (Iterator it = _certs.iterator(); it.hasNext();)
            {
                Object obj = it.next();
                if (obj instanceof ASN1TaggedObject)
                {
                    ASN1TaggedObject tagged = (ASN1TaggedObject)obj;

                    if (tagged.getTagNo() == 1)
                    {
                        attrCertV1Found = true;
                    }
                    else if (tagged.getTagNo() == 2)
                    {
                        attrCertV2Found = true;
                    }
                    else if (tagged.getTagNo() == 3)
                    {
                        otherCert = true;
                    }
                }
            }
        }

        if (otherCert)
        {
            return new DERInteger(5);
        }

        if (_crls != null && !otherCert)         // no need to check if otherCert is true
        {
            for (Iterator it = _crls.iterator(); it.hasNext();)
            {
                Object obj = it.next();
                if (obj instanceof ASN1TaggedObject)
                {
                    otherCrl = true;
                }
            }
        }

        if (otherCrl)
        {
            return new DERInteger(5);
        }

        if (attrCertV2Found)
        {
            return new DERInteger(4);
        }

        if (attrCertV1Found)
        {
            return new DERInteger(3);
        }

        if (contentOid.equals(DATA))
        {
            if (checkForVersion3(_signers))
            {
                return new DERInteger(3);
            }
            else
            {
                return new DERInteger(1);
            }
        }
        else
        {
            return new DERInteger(3);
        }
    
private booleancheckForVersion3(java.util.List signerInfos)

        for (Iterator it = signerInfos.iterator(); it.hasNext();)
        {
            SignerInfo s = SignerInfo.getInstance(((SignerInformation)it.next()).toSignerInfo());

            if (s.getVersion().getValue().intValue() == 3)
            {
                return true;
            }
        }

        return false;
    
private org.bouncycastle.asn1.x509.AlgorithmIdentifiermakeAlgId(java.lang.String oid, byte[] params)

        if (params != null)
        {
            return new AlgorithmIdentifier(
                            new DERObjectIdentifier(oid), makeObj(params));
        }
        else
        {
            return new AlgorithmIdentifier(
                            new DERObjectIdentifier(oid), new DERNull());
        }
    
private org.bouncycastle.asn1.DERObjectmakeObj(byte[] encoding)

        if (encoding == null)
        {
            return null;
        }

        ASN1InputStream         aIn = new ASN1InputStream(encoding);

        return aIn.readObject();
    
public java.io.OutputStreamopen(java.io.OutputStream out)
generate a signed object that for a CMS Signed Data object using the given provider.

        return open(out, false);
    
public java.io.OutputStreamopen(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".

        return open(out, DATA, encapsulate);
    
public java.io.OutputStreamopen(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". If dataOutputStream is non null the data being signed will be written to the stream as it is processed.

param
out stream the CMS object is to be written to.
param
encapsulate true if data should be encapsulated.
param
dataOutputStream output stream to copy the data being signed to.

        return open(out, DATA, encapsulate, dataOutputStream);
    
public java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String signedContentType, 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. The content type is set according to the OID represented by the string signedContentType.

        return open(out, signedContentType, encapsulate, null);
    
public java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String signedContentType, 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. The content type is set according to the OID represented by the string signedContentType.

param
out stream the CMS object is to be written to.
param
signedContentType OID for data to be signed.
param
encapsulate true if data should be encapsulated.
param
dataOutputStream output stream to copy the data being signed to.

        //
        // ContentInfo
        //
        BERSequenceGenerator sGen = new BERSequenceGenerator(out);
        
        sGen.addObject(CMSObjectIdentifiers.signedData);
        
        //
        // Signed Data
        //
        BERSequenceGenerator sigGen = new BERSequenceGenerator(sGen.getRawOutputStream(), 0, true);
        
        sigGen.addObject(calculateVersion(signedContentType));
        
        ASN1EncodableVector  digestAlgs = new ASN1EncodableVector();
        
        //
        // add the precalculated SignerInfo digest algorithms.
        //
        for (Iterator it = _signers.iterator(); it.hasNext();)
        {
            SignerInformation        signer = (SignerInformation)it.next();
            AlgorithmIdentifier     digAlgId;

            digAlgId = makeAlgId(signer.getDigestAlgOID(), signer.getDigestAlgParams());

            digestAlgs.add(digAlgId);
        }
        
        //
        // add the new digests
        //
        for (Iterator it = _signerInfs.iterator(); it.hasNext();)
        {
            SignerInf           signer = (SignerInf)it.next();
            AlgorithmIdentifier digAlgId;

            digAlgId = makeAlgId(signer.getDigestAlgOID(), signer.getDigestAlgParams());

            digestAlgs.add(digAlgId);
        }
        
        sigGen.getRawOutputStream().write(new DERSet(digestAlgs).getEncoded());
        
        BERSequenceGenerator eiGen = new BERSequenceGenerator(sigGen.getRawOutputStream());
        
        eiGen.addObject(new DERObjectIdentifier(signedContentType));
        
        OutputStream digStream;
        
        if (encapsulate)
        {
            BEROctetStringGenerator octGen = new BEROctetStringGenerator(eiGen.getRawOutputStream(), 0, true);
            
            if (_bufferSize != 0)
            {
                digStream = octGen.getOctetOutputStream(new byte[_bufferSize]);
            }
            else
            {
                digStream = octGen.getOctetOutputStream();
            }

            if (dataOutputStream != null)
            {
                digStream = new TeeOutputStream(dataOutputStream, digStream);
            }
        }
        else
        {
            if (dataOutputStream != null)
            {
                digStream = dataOutputStream;
            }
            else
            {
                digStream = new NullOutputStream();
            }
        }


        for (Iterator it = _messageDigests.iterator(); it.hasNext();)
        {
            digStream = new DigestOutputStream(digStream, (MessageDigest)it.next());
        }
        
        return new CmsSignedDataOutputStream(digStream, signedContentType, sGen, sigGen, eiGen);
    
public voidsetBufferSize(int bufferSize)
Set the underlying string size for encapsulated data

param
bufferSize length of octet strings to buffer the data.

        _bufferSize = bufferSize;