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

package org.bouncycastle.cms;

import org.bouncycastle.asn1.cms.AttributeTable;
import org.bouncycastle.util.Arrays;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.cms.EncryptedContentInfo;
import java.io.OutputStream;
import org.bouncycastle.asn1.cms.AuthEnvelopedData;
import java.io.InputStream;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.util.Encodable;

public class CMSAuthEnvelopedData implements Encodable
{
    RecipientInformationStore recipientInfoStore;
    ContentInfo contentInfo;
    private OriginatorInformation originatorInfo;
    private AlgorithmIdentifier authEncAlg;
    private ASN1Set authAttrs;
    private byte[] mac;
    private ASN1Set unauthAttrs;
    
    public CMSAuthEnvelopedData(final byte[] array) throws CMSException {
        this(CMSUtils.readContentInfo(array));
    }
    
    public CMSAuthEnvelopedData(final InputStream inputStream) throws CMSException {
        this(CMSUtils.readContentInfo(inputStream));
    }
    
    public CMSAuthEnvelopedData(final ContentInfo contentInfo) throws CMSException {
        this.contentInfo = contentInfo;
        final AuthEnvelopedData instance = AuthEnvelopedData.getInstance(contentInfo.getContent());
        if (instance.getOriginatorInfo() != null) {
            this.originatorInfo = new OriginatorInformation(instance.getOriginatorInfo());
        }
        final ASN1Set recipientInfos = instance.getRecipientInfos();
        final EncryptedContentInfo authEncryptedContentInfo = instance.getAuthEncryptedContentInfo();
        this.authEncAlg = authEncryptedContentInfo.getContentEncryptionAlgorithm();
        this.mac = instance.getMac().getOctets();
        final CMSSecureReadableWithAAD cmsSecureReadableWithAAD = new CMSSecureReadableWithAAD() {
            private OutputStream aadStream;
            
            @Override
            public ASN1Set getAuthAttrSet() {
                return CMSAuthEnvelopedData.this.authAttrs;
            }
            
            @Override
            public void setAuthAttrSet(final ASN1Set set) {
            }
            
            @Override
            public boolean hasAdditionalData() {
                return this.aadStream != null && CMSAuthEnvelopedData.this.authAttrs != null;
            }
            
            @Override
            public ASN1ObjectIdentifier getContentType() {
                return authEncryptedContentInfo.getContentType();
            }
            
            @Override
            public InputStream getInputStream() throws IOException {
                if (this.aadStream != null && CMSAuthEnvelopedData.this.authAttrs != null) {
                    this.aadStream.write(CMSAuthEnvelopedData.this.authAttrs.getEncoded("DER"));
                }
                return new InputStreamWithMAC(new ByteArrayInputStream(authEncryptedContentInfo.getEncryptedContent().getOctets()), CMSAuthEnvelopedData.this.mac);
            }
            
            @Override
            public void setAADStream(final OutputStream aadStream) {
                this.aadStream = aadStream;
            }
            
            @Override
            public OutputStream getAADStream() {
                return this.aadStream;
            }
            
            @Override
            public byte[] getMAC() {
                return Arrays.clone(CMSAuthEnvelopedData.this.mac);
            }
        };
        this.authAttrs = instance.getAuthAttrs();
        this.unauthAttrs = instance.getUnauthAttrs();
        this.recipientInfoStore = CMSEnvelopedHelper.buildRecipientInformationStore(recipientInfos, this.authEncAlg, cmsSecureReadableWithAAD);
    }
    
    public String getEncryptionAlgOID() {
        return this.authEncAlg.getAlgorithm().getId();
    }
    
    public OriginatorInformation getOriginatorInfo() {
        return this.originatorInfo;
    }
    
    public RecipientInformationStore getRecipientInfos() {
        return this.recipientInfoStore;
    }
    
    public AttributeTable getAuthAttrs() {
        if (this.authAttrs == null) {
            return null;
        }
        return new AttributeTable(this.authAttrs);
    }
    
    public AttributeTable getUnauthAttrs() {
        if (this.unauthAttrs == null) {
            return null;
        }
        return new AttributeTable(this.unauthAttrs);
    }
    
    public byte[] getMac() {
        return Arrays.clone(this.mac);
    }
    
    public ContentInfo toASN1Structure() {
        return this.contentInfo;
    }
    
    @Override
    public byte[] getEncoded() throws IOException {
        return this.contentInfo.getEncoded();
    }
}
