package org.bouncycastle.operator.jcajce;

import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.ProviderException;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.jcajce.util.DefaultJcaJceHelper;
import org.bouncycastle.jcajce.util.NamedJcaJceHelper;
import org.bouncycastle.jcajce.util.ProviderJcaJceHelper;
import org.bouncycastle.operator.AsymmetricKeyUnwrapper;
import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.operator.OperatorException;

/* JADX INFO: loaded from: classes2.dex */
public class JceAsymmetricKeyUnwrapper extends AsymmetricKeyUnwrapper {
    private Map extraMappings;
    private OperatorHelper helper;
    private PrivateKey privKey;
    private boolean unwrappedKeyMustBeEncodable;

    public JceAsymmetricKeyUnwrapper(AlgorithmIdentifier algorithmIdentifier, PrivateKey privateKey) {
        super(algorithmIdentifier);
        this.helper = new OperatorHelper(new DefaultJcaJceHelper());
        this.extraMappings = new HashMap();
        this.privKey = privateKey;
    }

    /* JADX WARN: Code duplicated, block: B:17:0x0052  */
    /* JADX WARN: Code duplicated, block: B:55:0x0048 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Code duplicated, block: B:8:0x0030 A[Catch: ProviderException -> 0x005e, UnsupportedOperationException -> 0x0060, IllegalStateException -> 0x0062, GeneralSecurityException -> 0x0064, InvalidAlgorithmParameterException -> 0x008a, BadPaddingException -> 0x00a8, IllegalBlockSizeException -> 0x00c6, InvalidKeyException -> 0x00e4, TryCatch #7 {IllegalStateException -> 0x0062, UnsupportedOperationException -> 0x0060, GeneralSecurityException -> 0x0064, ProviderException -> 0x005e, blocks: (B:5:0x001a, B:7:0x002a, B:9:0x0035, B:8:0x0030), top: B:57:0x001a }] */
    @Override // org.bouncycastle.operator.KeyUnwrapper
    public GenericKey generateUnwrappedKey(AlgorithmIdentifier algorithmIdentifier, byte[] bArr) throws OperatorException {
        Key keyUnwrap;
        byte[] encoded;
        try {
            Cipher cipherCreateAsymmetricWrapper = this.helper.createAsymmetricWrapper(getAlgorithmIdentifier(), this.extraMappings);
            AlgorithmParameters algorithmParametersCreateAlgorithmParameters = this.helper.createAlgorithmParameters(getAlgorithmIdentifier());
            Key secretKeySpec = null;
            if (algorithmParametersCreateAlgorithmParameters != null) {
                try {
                    if (getAlgorithmIdentifier().getAlgorithm().equals((ASN1Primitive) OIWObjectIdentifiers.elGamalAlgorithm)) {
                        cipherCreateAsymmetricWrapper.init(4, this.privKey);
                    } else {
                        cipherCreateAsymmetricWrapper.init(4, this.privKey, algorithmParametersCreateAlgorithmParameters);
                    }
                    keyUnwrap = cipherCreateAsymmetricWrapper.unwrap(bArr, this.helper.getKeyAlgorithmName(algorithmIdentifier.getAlgorithm()), 3);
                    try {
                        if (this.unwrappedKeyMustBeEncodable) {
                            try {
                                encoded = keyUnwrap.getEncoded();
                                if (encoded != null && encoded.length != 0) {
                                    secretKeySpec = keyUnwrap;
                                }
                            } catch (Exception e) {
                            }
                        } else {
                            secretKeySpec = keyUnwrap;
                        }
                    } catch (IllegalStateException e2) {
                    } catch (UnsupportedOperationException e3) {
                    } catch (GeneralSecurityException e4) {
                    } catch (ProviderException e5) {
                    }
                } catch (IllegalStateException e6) {
                } catch (UnsupportedOperationException e7) {
                } catch (GeneralSecurityException e8) {
                } catch (ProviderException e9) {
                }
            } else {
                cipherCreateAsymmetricWrapper.init(4, this.privKey);
                keyUnwrap = cipherCreateAsymmetricWrapper.unwrap(bArr, this.helper.getKeyAlgorithmName(algorithmIdentifier.getAlgorithm()), 3);
                if (this.unwrappedKeyMustBeEncodable) {
                    encoded = keyUnwrap.getEncoded();
                    if (encoded != null) {
                        secretKeySpec = keyUnwrap;
                    }
                } else {
                    secretKeySpec = keyUnwrap;
                }
            }
            if (secretKeySpec == null) {
                PrivateKey privateKey = this.privKey;
                if (algorithmParametersCreateAlgorithmParameters != null) {
                    cipherCreateAsymmetricWrapper.init(2, privateKey, algorithmParametersCreateAlgorithmParameters);
                } else {
                    cipherCreateAsymmetricWrapper.init(2, privateKey);
                }
                secretKeySpec = new SecretKeySpec(cipherCreateAsymmetricWrapper.doFinal(bArr), algorithmIdentifier.getAlgorithm().getId());
            }
            return new JceGenericKey(algorithmIdentifier, secretKeySpec);
        } catch (InvalidAlgorithmParameterException e10) {
            throw new OperatorException("invalid algorithm parameters: " + e10.getMessage(), e10);
        } catch (InvalidKeyException e11) {
            throw new OperatorException("key invalid: " + e11.getMessage(), e11);
        } catch (BadPaddingException e12) {
            throw new OperatorException("bad padding: " + e12.getMessage(), e12);
        } catch (IllegalBlockSizeException e13) {
            throw new OperatorException("illegal blocksize: " + e13.getMessage(), e13);
        }
    }

    public JceAsymmetricKeyUnwrapper setAlgorithmMapping(ASN1ObjectIdentifier aSN1ObjectIdentifier, String str) {
        this.extraMappings.put(aSN1ObjectIdentifier, str);
        return this;
    }

    public JceAsymmetricKeyUnwrapper setMustProduceEncodableUnwrappedKey(boolean z) {
        this.unwrappedKeyMustBeEncodable = z;
        return this;
    }

    public JceAsymmetricKeyUnwrapper setProvider(String str) {
        this.helper = new OperatorHelper(new NamedJcaJceHelper(str));
        return this;
    }

    public JceAsymmetricKeyUnwrapper setProvider(Provider provider) {
        this.helper = new OperatorHelper(new ProviderJcaJceHelper(provider));
        return this;
    }
}
