// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.cms;

import org.bouncycastle.asn1.ASN1Set;
import java.util.Collections;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import org.bouncycastle.asn1.BERSequenceGenerator;
import org.bouncycastle.asn1.ASN1EncodableVector;
import java.io.IOException;
import org.bouncycastle.operator.OutputAEADEncryptor;
import java.io.OutputStream;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;

public class CMSAuthEnvelopedDataStreamGenerator extends CMSAuthEnvelopedGenerator
{
    private int _bufferSize;
    private boolean _berEncodeRecipientSet;
    
    public void setBufferSize(final int bufferSize) {
        this._bufferSize = bufferSize;
    }
    
    public void setBEREncodeRecipients(final boolean berEncodeRecipientSet) {
        this._berEncodeRecipientSet = berEncodeRecipientSet;
    }
    
    private OutputStream doOpen(final ASN1ObjectIdentifier asn1ObjectIdentifier, final OutputStream outputStream, final OutputAEADEncryptor outputAEADEncryptor) throws IOException, CMSException {
        return this.open(asn1ObjectIdentifier, outputStream, CMSUtils.getRecipentInfos(outputAEADEncryptor.getKey(), this.recipientInfoGenerators), outputAEADEncryptor);
    }
    
    protected OutputStream open(final ASN1ObjectIdentifier asn1ObjectIdentifier, final OutputStream outputStream, final ASN1EncodableVector asn1EncodableVector, final OutputAEADEncryptor outputAEADEncryptor) throws IOException {
        final BERSequenceGenerator berSequenceGenerator = new BERSequenceGenerator(outputStream);
        berSequenceGenerator.addObject(CMSObjectIdentifiers.authEnvelopedData);
        final BERSequenceGenerator berSequenceGenerator2 = new BERSequenceGenerator(berSequenceGenerator.getRawOutputStream(), 0, true);
        berSequenceGenerator2.addObject(new ASN1Integer(0L));
        CMSUtils.addOriginatorInfoToGenerator(berSequenceGenerator2, this.originatorInfo);
        CMSUtils.addRecipientInfosToGenerator(asn1EncodableVector, berSequenceGenerator2, this._berEncodeRecipientSet);
        final BERSequenceGenerator berSequenceGenerator3 = new BERSequenceGenerator(berSequenceGenerator2.getRawOutputStream());
        berSequenceGenerator3.addObject(asn1ObjectIdentifier);
        berSequenceGenerator3.getRawOutputStream().write(outputAEADEncryptor.getAlgorithmIdentifier().getEncoded());
        return new CMSAuthEnvelopedDataOutputStream(outputAEADEncryptor, CMSUtils.createBEROctetOutputStream(berSequenceGenerator3.getRawOutputStream(), 0, true, this._bufferSize), berSequenceGenerator, berSequenceGenerator2, berSequenceGenerator3);
    }
    
    protected OutputStream open(final OutputStream outputStream, final ASN1EncodableVector asn1EncodableVector, final OutputAEADEncryptor outputAEADEncryptor) throws CMSException {
        try {
            return this.open(CMSObjectIdentifiers.data, outputStream, asn1EncodableVector, outputAEADEncryptor);
        }
        catch (final IOException ex) {
            throw new CMSException("exception decoding algorithm parameters.", ex);
        }
    }
    
    public OutputStream open(final OutputStream outputStream, final OutputAEADEncryptor outputAEADEncryptor) throws CMSException, IOException {
        return this.doOpen(new ASN1ObjectIdentifier(CMSObjectIdentifiers.data.getId()), outputStream, outputAEADEncryptor);
    }
    
    private class CMSAuthEnvelopedDataOutputStream extends OutputStream
    {
        private final OutputAEADEncryptor _encryptor;
        private final OutputStream _cOut;
        private final OutputStream _octetStream;
        private final BERSequenceGenerator _cGen;
        private final BERSequenceGenerator _envGen;
        private final BERSequenceGenerator _eiGen;
        
        public CMSAuthEnvelopedDataOutputStream(final OutputAEADEncryptor encryptor, final OutputStream octetStream, final BERSequenceGenerator cGen, final BERSequenceGenerator envGen, final BERSequenceGenerator eiGen) {
            this._encryptor = encryptor;
            this._octetStream = octetStream;
            this._cOut = encryptor.getOutputStream(octetStream);
            this._cGen = cGen;
            this._envGen = envGen;
            this._eiGen = eiGen;
        }
        
        @Override
        public void write(final int n) throws IOException {
            this._cOut.write(n);
        }
        
        @Override
        public void write(final byte[] b, final int off, final int len) throws IOException {
            this._cOut.write(b, off, len);
        }
        
        @Override
        public void write(final byte[] b) throws IOException {
            this._cOut.write(b);
        }
        
        @Override
        public void close() throws IOException {
            final ASN1Set processAuthAttrSet = CMSUtils.processAuthAttrSet(CMSAuthEnvelopedDataStreamGenerator.this.authAttrsGenerator, this._encryptor);
            this._cOut.close();
            this._octetStream.close();
            this._eiGen.close();
            if (processAuthAttrSet != null) {
                this._envGen.addObject(new DERTaggedObject(false, 1, processAuthAttrSet));
            }
            this._envGen.addObject(new DEROctetString(this._encryptor.getMAC()));
            CMSUtils.addAttriSetToGenerator(this._envGen, CMSAuthEnvelopedDataStreamGenerator.this.unauthAttrsGenerator, 2, Collections.EMPTY_MAP);
            this._envGen.close();
            this._cGen.close();
        }
    }
}
