package org.bouncycastle.cms;

import java.security.SecureRandom;
import java.security.Provider;
import java.security.AlgorithmParameterGenerator;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidParameterSpecException;
import java.io.OutputStream;
import java.io.IOException;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.RC2ParameterSpec;
import javax.crypto.spec.IvParameterSpec;

import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.DEREncodable;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.DERObjectIdentifier;

public class CMSAuthenticatedGenerator
    extends CMSEnvelopedGenerator
{
    /**
     * base constructor
     */
    public CMSAuthenticatedGenerator()
    {
    }

    /**
     * constructor allowing specific source of randomness
     *
     * @param rand instance of SecureRandom to use
     */
    public CMSAuthenticatedGenerator(
        SecureRandom rand)
    {
        super(rand);
    }

    protected AlgorithmIdentifier getAlgorithmIdentifier(String encryptionOID, AlgorithmParameterSpec paramSpec, Provider provider)
        throws IOException, NoSuchAlgorithmException, InvalidParameterSpecException
    {
        AlgorithmParameters params = CMSEnvelopedHelper.INSTANCE.createAlgorithmParameters(encryptionOID, provider);

        params.init(paramSpec);

        DEREncodable asn1Params;
        if (params != null)
        {
            ASN1InputStream aIn = new ASN1InputStream(params.getEncoded("ASN.1"));

            asn1Params = aIn.readObject();
        }
        else
        {
            asn1Params = new DERNull();
        }

        AlgorithmIdentifier encAlgId = new AlgorithmIdentifier(
            new DERObjectIdentifier(encryptionOID),
            asn1Params);
        return encAlgId;
    }

    protected AlgorithmParameterSpec generateParameterSpec(String encryptionOID, SecretKey encKey, Provider encProvider)
        throws CMSException
    {
        try
        {
            if (encryptionOID.equals(RC2_CBC))
            {
                byte[] iv = new byte[8];

                rand.nextBytes(iv);

                return new RC2ParameterSpec(encKey.getEncoded().length * 8, iv);
            }

            AlgorithmParameterGenerator pGen = CMSEnvelopedHelper.INSTANCE.createAlgorithmParameterGenerator(encryptionOID, encProvider);

            AlgorithmParameters p = pGen.generateParameters();

            return p.getParameterSpec(IvParameterSpec.class);
        }
        catch (GeneralSecurityException e)
        {
            return null;
        }
    }

    protected class MacOutputStream
        extends OutputStream
    {
        private final OutputStream out;
        private Mac mac;

        MacOutputStream(OutputStream out, Mac mac)
        {
            this.out = out;
            this.mac = mac;
        }

        public void write(byte[] buf)
            throws IOException
        {
            mac.update(buf, 0, buf.length);
            out.write(buf, 0, buf.length);
        }

        public void write(byte[] buf, int off, int len)
            throws IOException
        {
            mac.update(buf, off, len);
            out.write(buf, off, len);
        }

        public void write(int i)
            throws IOException
        {
            mac.update((byte)i);
            out.write(i);
        }

        public void close()
            throws IOException
        {
            out.close();
        }
        
        public byte[] getMac()
        {
            return mac.doFinal();
        }
    }
}

@@ -641,185 +641,8 @@
 ier;
-%0Aimport org.bouncycastle.asn1.DEREncodable;%0Aimport org.bouncycastle.asn1.ASN1InputStream;%0Aimport org.bouncycastle.asn1.DERNull;%0Aimport org.bouncycastle.asn1.DERObjectIdentifier;
 %0A%0Apu
@@ -1368,33 +1368,32 @@
 OID, provider);%0A
-%0A
         params.i
@@ -1421,446 +1421,60 @@
     
-DEREncodable asn1Params;%0A        if (params != null)%0A        %7B%0A            ASN1InputStream aIn = new ASN1InputStream(params.getEncoded(%22ASN.1%22));%0A%0A            asn1Params = aIn.readObject();%0A        %7D%0A        else%0A        %7B%0A            asn1Params = new DERNull();%0A        %7D%0A%0A        AlgorithmIdentifier encAlgId = new AlgorithmIdentifier(%0A            new DERObjectIdentifier(encryptionOID),%0A            asn1Params);%0A        return encAlgId
+return getAlgorithmIdentifier(encryptionOID, params)
 ;%0A  

Refactor to use existing method in CMSEnvelopedGenerator