package io.netty.handler.ssl;

import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.SSLException;
import javax.net.ssl.X509KeyManager;
import javax.security.auth.x500.X500Principal;
import org.apache.tomcat.jni.SSL;

/* JADX INFO: loaded from: classes.dex */
class OpenSslKeyMaterialManager {
    private static final Map<String, String> KEY_TYPES;
    static final String KEY_TYPE_DH_RSA = "DH_RSA";
    static final String KEY_TYPE_EC = "EC";
    static final String KEY_TYPE_EC_EC = "EC_EC";
    static final String KEY_TYPE_EC_RSA = "EC_RSA";
    static final String KEY_TYPE_RSA = "RSA";
    private final X509KeyManager keyManager;
    private final String password;

    static {
        HashMap map = new HashMap();
        KEY_TYPES = map;
        map.put(KEY_TYPE_RSA, KEY_TYPE_RSA);
        KEY_TYPES.put("DHE_RSA", KEY_TYPE_RSA);
        KEY_TYPES.put("ECDHE_RSA", KEY_TYPE_RSA);
        KEY_TYPES.put("ECDHE_ECDSA", KEY_TYPE_EC);
        KEY_TYPES.put("ECDH_RSA", KEY_TYPE_EC_RSA);
        KEY_TYPES.put("ECDH_ECDSA", KEY_TYPE_EC_EC);
        KEY_TYPES.put(KEY_TYPE_DH_RSA, KEY_TYPE_DH_RSA);
    }

    OpenSslKeyMaterialManager(X509KeyManager x509KeyManager, String str) {
        this.keyManager = x509KeyManager;
        this.password = str;
    }

    protected String chooseClientAlias(OpenSslEngine openSslEngine, String[] strArr, X500Principal[] x500PrincipalArr) {
        return this.keyManager.chooseClientAlias(strArr, x500PrincipalArr, null);
    }

    protected String chooseServerAlias(OpenSslEngine openSslEngine, String str) {
        return this.keyManager.chooseServerAlias(str, null, null);
    }

    void setKeyMaterial(OpenSslEngine openSslEngine) throws Exception {
        long jSslPointer = openSslEngine.sslPointer();
        for (String str : SSL.authenticationMethods(jSslPointer)) {
            String str2 = KEY_TYPES.get(str);
            if (str2 != null) {
                setKeyMaterial(jSslPointer, chooseServerAlias(openSslEngine, str2));
            }
        }
    }

    void setKeyMaterial(OpenSslEngine openSslEngine, String[] strArr, X500Principal[] x500PrincipalArr) throws Exception {
        setKeyMaterial(openSslEngine.sslPointer(), chooseClientAlias(openSslEngine, strArr, x500PrincipalArr));
    }

    private void setKeyMaterial(long j2, String str) throws Exception {
        long bio;
        long j3;
        long bio2;
        try {
            try {
                PrivateKey privateKey = this.keyManager.getPrivateKey(str);
                X509Certificate[] certificateChain = this.keyManager.getCertificateChain(str);
                if (certificateChain == null || certificateChain.length == 0) {
                    bio = 0;
                    bio2 = 0;
                } else {
                    bio = OpenSslContext.toBIO(this.keyManager.getCertificateChain(str));
                    if (privateKey != null) {
                        try {
                            bio2 = OpenSslContext.toBIO(privateKey);
                        } catch (SSLException e2) {
                            throw e2;
                        } catch (Exception e3) {
                            e = e3;
                            throw new SSLException(e);
                        } catch (Throwable th) {
                            th = th;
                            j3 = 0;
                            if (j3 != 0) {
                                SSL.freeBIO(j3);
                            }
                            if (bio != 0) {
                                SSL.freeBIO(bio);
                            }
                            throw th;
                        }
                    } else {
                        bio2 = 0;
                    }
                    try {
                        SSL.setCertificateBio(j2, bio, bio2, this.password);
                        SSL.setCertificateChainBio(j2, bio, false);
                    } catch (SSLException e4) {
                        throw e4;
                    } catch (Exception e5) {
                        e = e5;
                        throw new SSLException(e);
                    }
                }
                if (bio2 != 0) {
                    SSL.freeBIO(bio2);
                }
                if (bio != 0) {
                    SSL.freeBIO(bio);
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (SSLException e6) {
            throw e6;
        } catch (Exception e7) {
            e = e7;
        } catch (Throwable th3) {
            th = th3;
            bio = 0;
            j3 = 0;
        }
    }
}
