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

package org.bouncycastle.cms.jcajce;

import java.security.AccessController;
import java.security.PrivilegedAction;
import org.bouncycastle.cms.InputStreamWithMAC;
import java.io.OutputStream;
import org.bouncycastle.jcajce.io.CipherInputStream;
import java.io.InputStream;
import javax.crypto.Cipher;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.operator.InputAEADDecryptor;

class CMSInputAEADDecryptor implements InputAEADDecryptor
{
    private final AlgorithmIdentifier contentEncryptionAlgorithm;
    private final Cipher dataCipher;
    private InputStream inputStream;
    
    CMSInputAEADDecryptor(final AlgorithmIdentifier contentEncryptionAlgorithm, final Cipher dataCipher) {
        this.contentEncryptionAlgorithm = contentEncryptionAlgorithm;
        this.dataCipher = dataCipher;
    }
    
    @Override
    public AlgorithmIdentifier getAlgorithmIdentifier() {
        return this.contentEncryptionAlgorithm;
    }
    
    @Override
    public InputStream getInputStream(final InputStream inputStream) {
        this.inputStream = inputStream;
        return new CipherInputStream(inputStream, this.dataCipher);
    }
    
    @Override
    public OutputStream getAADStream() {
        if (checkForAEAD()) {
            return new JceAADStream(this.dataCipher);
        }
        return null;
    }
    
    @Override
    public byte[] getMAC() {
        if (this.inputStream instanceof InputStreamWithMAC) {
            return ((InputStreamWithMAC)this.inputStream).getMAC();
        }
        return null;
    }
    
    private static boolean checkForAEAD() {
        return AccessController.doPrivileged((PrivilegedAction<Boolean>)new PrivilegedAction() {
            @Override
            public Object run() {
                try {
                    return Cipher.class.getMethod("updateAAD", byte[].class) != null;
                }
                catch (final Exception ex) {
                    return Boolean.FALSE;
                }
            }
        });
    }
}
