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

CMSEnvelopedDataStreamGenerator

public class CMSEnvelopedDataStreamGenerator extends CMSEnvelopedGenerator
General class for generating a CMS enveloped-data message stream.

A simple example of usage.

CMSEnvelopedDataStreamGenerator edGen = new CMSEnvelopedDataStreamGenerator();

edGen.addKeyTransRecipient(cert);

ByteArrayOutputStream bOut = new ByteArrayOutputStream();

OutputStream out = edGen.open(
bOut, CMSEnvelopedDataGenerator.AES128_CBC, "BC");*
out.write(data);

out.close();

Fields Summary
private Object
_originatorInfo
private Object
_unprotectedAttributes
private int
_bufferSize
private boolean
_berEncodeRecipientSet
Constructors Summary
public CMSEnvelopedDataStreamGenerator()
base constructor


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

param
rand instance of SecureRandom to use

        super(rand);
    
Methods Summary
private org.bouncycastle.asn1.DERIntegergetVersion()

        if (_originatorInfo != null || _unprotectedAttributes != null)
        {
            return new DERInteger(2);
        }
        else
        {
            return new DERInteger(0);
        }
    
public java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String encryptionOID, java.security.Provider provider)

        KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE.createSymmetricKeyGenerator(encryptionOID, provider);

        keyGen.init(rand);

        return open(out, encryptionOID, keyGen, provider);
    
public java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String encryptionOID, int keySize, java.lang.String provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider.

        return open(out, encryptionOID, keySize, CMSUtils.getProvider(provider));
    
public java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String encryptionOID, int keySize, java.security.Provider provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider.

        KeyGenerator keyGen = CMSEnvelopedHelper.INSTANCE.createSymmetricKeyGenerator(encryptionOID, provider);

        keyGen.init(keySize, rand);

        return open(out, encryptionOID, keyGen, provider);
    
private java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String encryptionOID, javax.crypto.KeyGenerator keyGen, java.security.Provider provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider and the passed in key generator.

throws
IOException

        Provider            encProvider = keyGen.getProvider();
        SecretKey           encKey = keyGen.generateKey();
        AlgorithmParameters params = generateParameters(encryptionOID, encKey, encProvider);

        Iterator            it = recipientInfs.iterator();
        ASN1EncodableVector recipientInfos = new ASN1EncodableVector();
        
        while (it.hasNext())
        {
            RecipientInf            recipient = (RecipientInf)it.next();

            try
            {
                recipientInfos.add(recipient.toRecipientInfo(encKey, rand, provider));
            }
            catch (IOException e)
            {
                throw new CMSException("encoding error.", e);
            }
            catch (InvalidKeyException e)
            {
                throw new CMSException("key inappropriate for algorithm.", e);
            }
            catch (GeneralSecurityException e)
            {
                throw new CMSException("error making encrypted content.", e);
            }
        }
        
        return open(out, encryptionOID, encKey, params, recipientInfos, encProvider);
    
protected java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String encryptionOID, javax.crypto.SecretKey encKey, java.security.AlgorithmParameters params, org.bouncycastle.asn1.ASN1EncodableVector recipientInfos, java.lang.String provider)

        return open(out, encryptionOID, encKey, params, recipientInfos, CMSUtils.getProvider(provider));
    
protected java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String encryptionOID, javax.crypto.SecretKey encKey, java.security.AlgorithmParameters params, org.bouncycastle.asn1.ASN1EncodableVector recipientInfos, java.security.Provider provider)

        try
        {
            //
            // ContentInfo
            //
            BERSequenceGenerator cGen = new BERSequenceGenerator(out);
            
            cGen.addObject(CMSObjectIdentifiers.envelopedData);
            
            //
            // Encrypted Data
            //
            BERSequenceGenerator envGen = new BERSequenceGenerator(cGen.getRawOutputStream(), 0, true);
            
            envGen.addObject(getVersion());

            if (_berEncodeRecipientSet)
            {
                envGen.getRawOutputStream().write(new BERSet(recipientInfos).getEncoded());
            }
            else
            {
                envGen.getRawOutputStream().write(new DERSet(recipientInfos).getEncoded());
            }

            Cipher cipher = CMSEnvelopedHelper.INSTANCE.getSymmetricCipher(encryptionOID, provider);
            
            cipher.init(Cipher.ENCRYPT_MODE, encKey, params, rand);

            BERSequenceGenerator eiGen = new BERSequenceGenerator(envGen.getRawOutputStream());
            
            eiGen.addObject(PKCSObjectIdentifiers.data);

            //
            // If params are null we try and second guess on them as some providers don't provide
            // algorithm parameter generation explicitly but instead generate them under the hood.
            //
            if (params == null)
            {
                params = cipher.getParameters();
            }

            AlgorithmIdentifier encAlgId = getAlgorithmIdentifier(encryptionOID, params);
                        
            eiGen.getRawOutputStream().write(encAlgId.getEncoded());
            
            BEROctetStringGenerator octGen = new BEROctetStringGenerator(eiGen.getRawOutputStream(), 0, false);
            
            CipherOutputStream      cOut;
            
            if (_bufferSize != 0)
            {
                cOut = new CipherOutputStream(octGen.getOctetOutputStream(new byte[_bufferSize]), cipher);
            }
            else
            {
                cOut = new CipherOutputStream(octGen.getOctetOutputStream(), cipher);
            }

            return new CmsEnvelopedDataOutputStream(cOut, cGen, envGen, eiGen);
        }
        catch (InvalidKeyException e)
        {
            throw new CMSException("key invalid in message.", e);
        }
        catch (NoSuchPaddingException e)
        {
            throw new CMSException("required padding not supported.", e);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new CMSException("algorithm parameters invalid.", e);
        }
        catch (IOException e)
        {
            throw new CMSException("exception decoding algorithm parameters.", e);
        }
    
public java.io.OutputStreamopen(java.io.OutputStream out, java.lang.String encryptionOID, java.lang.String provider)
generate an enveloped object that contains an CMS Enveloped Data object using the given provider.

throws
IOException

        return open(out, encryptionOID, CMSUtils.getProvider(provider));
    
public voidsetBEREncodeRecipients(boolean berEncodeRecipientSet)
Use a BER Set to store the recipient information

        _berEncodeRecipientSet = berEncodeRecipientSet;
    
public voidsetBufferSize(int bufferSize)
Set the underlying string size for encapsulated data

param
bufferSize length of octet strings to buffer the data.

        _bufferSize = bufferSize;