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

package org.bouncycastle.cms.jcajce;

import javax.crypto.Cipher;
import org.bouncycastle.operator.OperatorException;
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 org.bouncycastle.asn1.cms.CMSORIforKEMOtherInfo;
import org.bouncycastle.operator.GenericKey;
import java.security.Provider;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import java.util.HashMap;
import org.bouncycastle.operator.DefaultKemEncapsulationLengthProvider;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import org.bouncycastle.asn1.iso.ISOIECObjectIdentifiers;
import java.security.interfaces.RSAPublicKey;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import java.security.SecureRandom;
import java.security.PublicKey;
import java.util.Map;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.operator.KemEncapsulationLengthProvider;
import org.bouncycastle.cms.KEMKeyWrapper;

class JceCMSKEMKeyWrapper extends KEMKeyWrapper
{
    private final KemEncapsulationLengthProvider kemEncLenProvider;
    private final AlgorithmIdentifier symWrapAlgorithm;
    private final int kekLength;
    private JcaJceExtHelper helper;
    private Map extraMappings;
    private PublicKey publicKey;
    private SecureRandom random;
    private AlgorithmIdentifier kdfAlgorithm;
    private byte[] encapsulation;
    
    public JceCMSKEMKeyWrapper(final PublicKey publicKey, final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        super((publicKey instanceof RSAPublicKey) ? new AlgorithmIdentifier(ISOIECObjectIdentifiers.id_kem_rsa) : SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()).getAlgorithm());
        this.kemEncLenProvider = new DefaultKemEncapsulationLengthProvider();
        this.helper = new DefaultJcaJceExtHelper();
        this.extraMappings = new HashMap();
        this.kdfAlgorithm = new AlgorithmIdentifier(X9ObjectIdentifiers.id_kdf_kdf3, new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256, DERNull.INSTANCE));
        this.publicKey = publicKey;
        this.symWrapAlgorithm = new AlgorithmIdentifier(asn1ObjectIdentifier);
        this.kekLength = CMSUtils.getKekSize(asn1ObjectIdentifier);
    }
    
    public JceCMSKEMKeyWrapper setProvider(final Provider provider) {
        this.helper = new ProviderJcaJceExtHelper(provider);
        return this;
    }
    
    public JceCMSKEMKeyWrapper setProvider(final String s) {
        this.helper = new NamedJcaJceExtHelper(s);
        return this;
    }
    
    public JceCMSKEMKeyWrapper setKDF(final AlgorithmIdentifier kdfAlgorithm) {
        this.kdfAlgorithm = kdfAlgorithm;
        return this;
    }
    
    public JceCMSKEMKeyWrapper setSecureRandom(final SecureRandom random) {
        this.random = random;
        return this;
    }
    
    public JceCMSKEMKeyWrapper setAlgorithmMapping(final ASN1ObjectIdentifier asn1ObjectIdentifier, final String s) {
        this.extraMappings.put(asn1ObjectIdentifier, s);
        return this;
    }
    
    @Override
    public byte[] getEncapsulation() {
        return this.encapsulation;
    }
    
    @Override
    public AlgorithmIdentifier getKdfAlgorithmIdentifier() {
        return this.kdfAlgorithm;
    }
    
    @Override
    public int getKekLength() {
        return this.kekLength;
    }
    
    @Override
    public AlgorithmIdentifier getWrapAlgorithmIdentifier() {
        return this.symWrapAlgorithm;
    }
    
    @Override
    public byte[] generateWrappedKey(final GenericKey genericKey) throws OperatorException {
        try {
            final byte[] encoded = new CMSORIforKEMOtherInfo(this.symWrapAlgorithm, this.kekLength).getEncoded();
            if (this.publicKey instanceof RSAPublicKey) {
                final Cipher asymmetricWrapper = CMSUtils.createAsymmetricWrapper(this.helper, this.getAlgorithmIdentifier().getAlgorithm(), new HashMap());
                try {
                    asymmetricWrapper.init(3, this.publicKey, new KTSParameterSpec.Builder(CMSUtils.getWrapAlgorithmName(this.symWrapAlgorithm.getAlgorithm()), this.kekLength * 8, encoded).withKdfAlgorithm(this.kdfAlgorithm).build(), this.random);
                    final byte[] wrap = asymmetricWrapper.wrap(CMSUtils.getJceKey(genericKey));
                    final int n = (((RSAPublicKey)this.publicKey).getModulus().bitLength() + 7) / 8;
                    this.encapsulation = Arrays.copyOfRange(wrap, 0, n);
                    return Arrays.copyOfRange(wrap, n, wrap.length);
                }
                catch (final Exception ex) {
                    throw new OperatorException("Unable to wrap contents key: " + ex.getMessage(), ex);
                }
            }
            final Cipher asymmetricWrapper2 = CMSUtils.createAsymmetricWrapper(this.helper, this.getAlgorithmIdentifier().getAlgorithm(), new HashMap());
            try {
                asymmetricWrapper2.init(3, this.publicKey, new KTSParameterSpec.Builder(CMSUtils.getWrapAlgorithmName(this.symWrapAlgorithm.getAlgorithm()), this.kekLength * 8, encoded).withKdfAlgorithm(this.kdfAlgorithm).build(), this.random);
                final byte[] wrap2 = asymmetricWrapper2.wrap(CMSUtils.getJceKey(genericKey));
                final int kemEncLength = this.getKemEncLength(this.publicKey);
                this.encapsulation = Arrays.copyOfRange(wrap2, 0, kemEncLength);
                return Arrays.copyOfRange(wrap2, kemEncLength, wrap2.length);
            }
            catch (final Exception ex2) {
                throw new OperatorException("Unable to wrap contents key: " + ex2.getMessage(), ex2);
            }
        }
        catch (final Exception ex3) {
            throw new OperatorException("unable to wrap contents key: " + ex3.getMessage(), ex3);
        }
    }
    
    private int getKemEncLength(final PublicKey publicKey) {
        return this.kemEncLenProvider.getEncapsulationLength(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()).getAlgorithm());
    }
}
