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

package org.bouncycastle.cms.jcajce;

import javax.crypto.Cipher;
import org.bouncycastle.operator.OperatorException;
import org.bouncycastle.operator.jcajce.JceGenericKey;
import org.bouncycastle.util.Arrays;
import java.security.spec.AlgorithmParameterSpec;
import java.security.Key;
import org.bouncycastle.jcajce.spec.KTSParameterSpec;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import java.security.interfaces.RSAPrivateKey;
import org.bouncycastle.asn1.cms.CMSORIforKEMOtherInfo;
import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import java.security.Provider;
import org.bouncycastle.asn1.cms.KEMRecipientInfo;
import java.util.HashMap;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import java.security.PrivateKey;
import java.util.Map;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.operator.AsymmetricKeyUnwrapper;

class JceCMSKEMKeyUnwrapper extends AsymmetricKeyUnwrapper
{
    private final AlgorithmIdentifier symWrapAlgorithm;
    private final int kekLength;
    private JcaJceExtHelper helper;
    private Map extraMappings;
    private PrivateKey privateKey;
    
    public JceCMSKEMKeyUnwrapper(final AlgorithmIdentifier symWrapAlgorithm, final PrivateKey privateKey) {
        super(PrivateKeyInfo.getInstance(privateKey.getEncoded()).getPrivateKeyAlgorithm());
        this.helper = new DefaultJcaJceExtHelper();
        this.extraMappings = new HashMap();
        final KEMRecipientInfo instance = KEMRecipientInfo.getInstance(symWrapAlgorithm.getParameters());
        this.privateKey = privateKey;
        this.symWrapAlgorithm = symWrapAlgorithm;
        this.kekLength = CMSUtils.getKekSize(instance.getWrap().getAlgorithm());
    }
    
    public JceCMSKEMKeyUnwrapper setProvider(final Provider provider) {
        this.helper = new ProviderJcaJceExtHelper(provider);
        return this;
    }
    
    public JceCMSKEMKeyUnwrapper setProvider(final String s) {
        this.helper = new NamedJcaJceExtHelper(s);
        return this;
    }
    
    public JceCMSKEMKeyUnwrapper setAlgorithmMapping(final ASN1ObjectIdentifier asn1ObjectIdentifier, final String s) {
        this.extraMappings.put(asn1ObjectIdentifier, s);
        return this;
    }
    
    public int getKekLength() {
        return this.kekLength;
    }
    
    @Override
    public GenericKey generateUnwrappedKey(final AlgorithmIdentifier algorithmIdentifier, final byte[] array) throws OperatorException {
        final KEMRecipientInfo instance = KEMRecipientInfo.getInstance(this.symWrapAlgorithm.getParameters());
        final AlgorithmIdentifier wrap = instance.getWrap();
        try {
            final byte[] encoded = new CMSORIforKEMOtherInfo(wrap, this.kekLength, instance.getUkm()).getEncoded();
            if (this.privateKey instanceof RSAPrivateKey) {
                final Cipher asymmetricWrapper = CMSUtils.createAsymmetricWrapper(this.helper, instance.getKem().getAlgorithm(), new HashMap());
                try {
                    final String wrapAlgorithmName = CMSUtils.getWrapAlgorithmName(wrap.getAlgorithm());
                    asymmetricWrapper.init(4, this.privateKey, new KTSParameterSpec.Builder(wrapAlgorithmName, this.kekLength * 8, encoded).withKdfAlgorithm(instance.getKdf()).build());
                    return new JceGenericKey(algorithmIdentifier, asymmetricWrapper.unwrap(Arrays.concatenate(instance.getKemct().getOctets(), instance.getEncryptedKey().getOctets()), wrapAlgorithmName, 3));
                }
                catch (final Exception ex) {
                    throw new OperatorException("Unable to wrap contents key: " + ex.getMessage(), ex);
                }
            }
            final Cipher asymmetricWrapper2 = CMSUtils.createAsymmetricWrapper(this.helper, instance.getKem().getAlgorithm(), new HashMap());
            final String wrapAlgorithmName2 = CMSUtils.getWrapAlgorithmName(wrap.getAlgorithm());
            asymmetricWrapper2.init(4, this.privateKey, new KTSParameterSpec.Builder(wrapAlgorithmName2, this.kekLength * 8, encoded).withKdfAlgorithm(instance.getKdf()).build());
            return new JceGenericKey(algorithmIdentifier, asymmetricWrapper2.unwrap(Arrays.concatenate(instance.getKemct().getOctets(), instance.getEncryptedKey().getOctets()), wrapAlgorithmName2, 3));
        }
        catch (final Exception ex2) {
            throw new OperatorException("exception encrypting key: " + ex2.getMessage(), ex2);
        }
    }
}
