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

package org.bouncycastle.cms;

import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.cms.CMSAttributes;
import org.bouncycastle.asn1.ASN1SetParser;
import org.bouncycastle.asn1.cms.OriginatorInfo;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.cms.EncryptedContentInfoParser;
import java.io.OutputStream;
import org.bouncycastle.asn1.ASN1OctetStringParser;
import org.bouncycastle.asn1.ASN1SequenceParser;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.cms.AttributeTable;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.asn1.cms.AuthEnvelopedDataParser;

public class CMSAuthEnvelopedDataParser extends CMSContentInfoParser
{
    private final RecipientInformationStore recipientInfoStore;
    private final AuthEnvelopedDataParser authEvnData;
    private final LocalMacProvider localMacProvider;
    private final AlgorithmIdentifier encAlg;
    private AttributeTable authAttrs;
    private ASN1Set authAttrSet;
    private AttributeTable unauthAttrs;
    private boolean authAttrNotRead;
    private boolean unauthAttrNotRead;
    private OriginatorInformation originatorInfo;
    
    public CMSAuthEnvelopedDataParser(final byte[] buf) throws CMSException, IOException {
        this(new ByteArrayInputStream(buf));
    }
    
    public CMSAuthEnvelopedDataParser(final InputStream inputStream) throws CMSException, IOException {
        super(inputStream);
        this.authAttrNotRead = true;
        this.unauthAttrNotRead = true;
        this.authEvnData = new AuthEnvelopedDataParser((ASN1SequenceParser)this._contentInfo.getContent(16));
        final OriginatorInfo originatorInfo = this.authEvnData.getOriginatorInfo();
        if (originatorInfo != null) {
            this.originatorInfo = new OriginatorInformation(originatorInfo);
        }
        final ASN1Set instance = ASN1Set.getInstance(this.authEvnData.getRecipientInfos().toASN1Primitive());
        final EncryptedContentInfoParser authEncryptedContentInfo = this.authEvnData.getAuthEncryptedContentInfo();
        this.encAlg = authEncryptedContentInfo.getContentEncryptionAlgorithm();
        this.localMacProvider = new LocalMacProvider(this.authEvnData, this);
        final CMSSecureReadableWithAAD secureReadable = new CMSSecureReadableWithAAD() {
            private OutputStream aadStream;
            final /* synthetic */ CMSReadable val$readable = new CMSProcessableInputStream(new InputStreamWithMAC(((ASN1OctetStringParser)authEncryptedContentInfo.getEncryptedContent(4)).getOctetStream(), CMSAuthEnvelopedDataParser.this.localMacProvider));
            
            @Override
            public ASN1ObjectIdentifier getContentType() {
                return authEncryptedContentInfo.getContentType();
            }
            
            @Override
            public InputStream getInputStream() throws IOException, CMSException {
                return this.val$readable.getInputStream();
            }
            
            @Override
            public ASN1Set getAuthAttrSet() {
                return CMSAuthEnvelopedDataParser.this.authAttrSet;
            }
            
            @Override
            public void setAuthAttrSet(final ASN1Set set) {
            }
            
            @Override
            public boolean hasAdditionalData() {
                return true;
            }
            
            @Override
            public void setAADStream(final OutputStream aadStream) {
                this.aadStream = aadStream;
            }
            
            @Override
            public OutputStream getAADStream() {
                return this.aadStream;
            }
            
            @Override
            public byte[] getMAC() {
                return Arrays.clone(CMSAuthEnvelopedDataParser.this.localMacProvider.getMAC());
            }
        };
        this.localMacProvider.setSecureReadable(secureReadable);
        this.recipientInfoStore = CMSEnvelopedHelper.buildRecipientInformationStore(instance, this.encAlg, secureReadable);
    }
    
    public OriginatorInformation getOriginatorInfo() {
        return this.originatorInfo;
    }
    
    public AlgorithmIdentifier getEncryptionAlgOID() {
        return this.encAlg;
    }
    
    public String getEncAlgOID() {
        return this.encAlg.getAlgorithm().toString();
    }
    
    public byte[] getEncAlgParams() {
        try {
            return CMSUtils.encodeObj(this.encAlg.getParameters());
        }
        catch (final Exception obj) {
            throw new RuntimeException("exception getting encryption parameters " + obj);
        }
    }
    
    public RecipientInformationStore getRecipientInfos() {
        return this.recipientInfoStore;
    }
    
    public byte[] getMac() throws IOException {
        return Arrays.clone(this.localMacProvider.getMAC());
    }
    
    private ASN1Set getAuthAttrSet() throws IOException {
        if (this.authAttrs == null && this.authAttrNotRead) {
            final ASN1SetParser authAttrs = this.authEvnData.getAuthAttrs();
            if (authAttrs != null) {
                this.authAttrSet = (ASN1Set)authAttrs.toASN1Primitive();
            }
            this.authAttrNotRead = false;
        }
        return this.authAttrSet;
    }
    
    public AttributeTable getAuthAttrs() throws IOException {
        if (this.authAttrs == null && this.authAttrNotRead) {
            final ASN1Set authAttrSet = this.getAuthAttrSet();
            if (authAttrSet != null) {
                this.authAttrs = new AttributeTable(authAttrSet);
            }
        }
        return this.authAttrs;
    }
    
    public AttributeTable getUnauthAttrs() throws IOException {
        if (this.unauthAttrs == null && this.unauthAttrNotRead) {
            this.unauthAttrNotRead = false;
            this.unauthAttrs = CMSUtils.getAttributesTable(this.authEvnData.getUnauthAttrs());
        }
        return this.unauthAttrs;
    }
    
    public byte[] getContentDigest() {
        if (this.authAttrs != null) {
            return ASN1OctetString.getInstance(this.authAttrs.get(CMSAttributes.messageDigest).getAttrValues().getObjectAt(0)).getOctets();
        }
        return null;
    }
    
    static class LocalMacProvider implements MACProvider
    {
        private byte[] mac;
        private final AuthEnvelopedDataParser authEnvData;
        private final CMSAuthEnvelopedDataParser parser;
        private CMSSecureReadableWithAAD readable;
        
        LocalMacProvider(final AuthEnvelopedDataParser authEnvData, final CMSAuthEnvelopedDataParser parser) {
            this.authEnvData = authEnvData;
            this.parser = parser;
        }
        
        @Override
        public void init() throws IOException {
            this.parser.authAttrs = this.parser.getAuthAttrs();
            if (this.parser.authAttrs != null) {
                this.readable.setAuthAttrSet(this.parser.authAttrSet);
                this.readable.getAADStream().write(this.parser.authAttrs.toASN1Structure().getEncoded("DER"));
            }
            this.mac = this.authEnvData.getMac().getOctets();
        }
        
        void setSecureReadable(final CMSSecureReadableWithAAD readable) {
            this.readable = readable;
        }
        
        @Override
        public byte[] getMAC() {
            return this.mac;
        }
    }
}
