package io.netty.handler.ssl;

import io.netty.buffer.ByteBufAllocator;
import io.netty.internal.tcnative.CertificateRequestedCallback;
import io.netty.internal.tcnative.SSL;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import javax.net.ssl.SSLException;
import javax.net.ssl.X509KeyManager;
import javax.security.auth.x500.X500Principal;

/* JADX INFO: loaded from: classes.dex */
public class OpenSslKeyMaterialManager {
    private static final Map<String, String> KEY_TYPES;
    public static final String KEY_TYPE_DH_RSA = "DH_RSA";
    public static final String KEY_TYPE_EC = "EC";
    public static final String KEY_TYPE_EC_EC = "EC_EC";
    public static final String KEY_TYPE_EC_RSA = "EC_RSA";
    public 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);
        map.put("DHE_RSA", KEY_TYPE_RSA);
        map.put("ECDHE_RSA", KEY_TYPE_RSA);
        map.put("ECDHE_ECDSA", KEY_TYPE_EC);
        map.put("ECDH_RSA", KEY_TYPE_EC_RSA);
        map.put("ECDH_ECDSA", KEY_TYPE_EC_EC);
        map.put(KEY_TYPE_DH_RSA, KEY_TYPE_DH_RSA);
    }

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

    private void setKeyMaterial(long j2, String str) throws Throwable {
        long bio = 0;
        try {
            try {
                X509Certificate[] certificateChain = this.keyManager.getCertificateChain(str);
                if (certificateChain != null && certificateChain.length != 0) {
                    try {
                        PrivateKey privateKey = this.keyManager.getPrivateKey(str);
                        ByteBufAllocator byteBufAllocator = ByteBufAllocator.DEFAULT;
                        PemEncoded pem = PemX509Certificate.toPEM(byteBufAllocator, true, certificateChain);
                        try {
                            long bio2 = ReferenceCountedOpenSslContext.toBIO(byteBufAllocator, pem.retain());
                            try {
                                long bio3 = ReferenceCountedOpenSslContext.toBIO(byteBufAllocator, pem.retain());
                                if (privateKey != null) {
                                    try {
                                        bio = ReferenceCountedOpenSslContext.toBIO(privateKey);
                                    } catch (Throwable th) {
                                        th = th;
                                        pem.release();
                                        throw th;
                                    }
                                }
                                try {
                                    SSL.setCertificateBio(j2, bio2, bio, this.password);
                                    SSL.setCertificateChainBio(j2, bio3, true);
                                    pem.release();
                                    ReferenceCountedOpenSslContext.freeBio(bio);
                                    ReferenceCountedOpenSslContext.freeBio(bio2);
                                    ReferenceCountedOpenSslContext.freeBio(bio3);
                                    return;
                                } catch (Throwable th2) {
                                    th = th2;
                                    pem.release();
                                    throw th;
                                }
                            } catch (Throwable th3) {
                                th = th3;
                            }
                        } catch (Throwable th4) {
                            th = th4;
                        }
                    } catch (SSLException e2) {
                        throw e2;
                    } catch (Exception e3) {
                        e = e3;
                        throw new SSLException(e);
                    }
                }
                ReferenceCountedOpenSslContext.freeBio(0L);
                ReferenceCountedOpenSslContext.freeBio(0L);
                ReferenceCountedOpenSslContext.freeBio(0L);
            } catch (Throwable th5) {
                th = th5;
                ReferenceCountedOpenSslContext.freeBio(0L);
                ReferenceCountedOpenSslContext.freeBio(0L);
                ReferenceCountedOpenSslContext.freeBio(0L);
                throw th;
            }
        } catch (SSLException e4) {
            throw e4;
        } catch (Exception e5) {
            e = e5;
        } catch (Throwable th6) {
            th = th6;
            ReferenceCountedOpenSslContext.freeBio(0L);
            ReferenceCountedOpenSslContext.freeBio(0L);
            ReferenceCountedOpenSslContext.freeBio(0L);
            throw th;
        }
    }

    public String chooseClientAlias(ReferenceCountedOpenSslEngine referenceCountedOpenSslEngine, String[] strArr, X500Principal[] x500PrincipalArr) {
        return this.keyManager.chooseClientAlias(strArr, x500PrincipalArr, null);
    }

    public String chooseServerAlias(ReferenceCountedOpenSslEngine referenceCountedOpenSslEngine, String str) {
        return this.keyManager.chooseServerAlias(str, null, null);
    }

    public CertificateRequestedCallback.KeyMaterial keyMaterial(ReferenceCountedOpenSslEngine referenceCountedOpenSslEngine, String[] strArr, X500Principal[] x500PrincipalArr) throws Throwable {
        long bio;
        long x509Chain;
        long j2;
        long j3;
        long privateKey;
        CertificateRequestedCallback.KeyMaterial keyMaterial;
        long bio2;
        String strChooseClientAlias = chooseClientAlias(referenceCountedOpenSslEngine, strArr, x500PrincipalArr);
        long j4 = 0;
        try {
            try {
                X509Certificate[] certificateChain = this.keyManager.getCertificateChain(strChooseClientAlias);
                if (certificateChain == null || certificateChain.length == 0) {
                    keyMaterial = null;
                    ReferenceCountedOpenSslContext.freeBio(0L);
                    ReferenceCountedOpenSslContext.freeBio(0L);
                } else {
                    PrivateKey privateKey2 = this.keyManager.getPrivateKey(strChooseClientAlias);
                    bio = ReferenceCountedOpenSslContext.toBIO(certificateChain);
                    try {
                        x509Chain = SSL.parseX509Chain(bio);
                        if (privateKey2 != null) {
                            try {
                                bio2 = ReferenceCountedOpenSslContext.toBIO(privateKey2);
                                try {
                                    privateKey = SSL.parsePrivateKey(bio2, this.password);
                                } catch (SSLException e2) {
                                    e = e2;
                                    throw e;
                                } catch (Exception e3) {
                                    e = e3;
                                    throw new SSLException(e);
                                } catch (Throwable th) {
                                    th = th;
                                    privateKey = 0;
                                    j4 = bio2;
                                    ReferenceCountedOpenSslContext.freeBio(j4);
                                    ReferenceCountedOpenSslContext.freeBio(bio);
                                    SSL.freePrivateKey(privateKey);
                                    SSL.freeX509Chain(x509Chain);
                                    throw th;
                                }
                            } catch (SSLException e4) {
                                throw e4;
                            } catch (Exception e5) {
                                e = e5;
                                throw new SSLException(e);
                            } catch (Throwable th2) {
                                th = th2;
                                privateKey = 0;
                                ReferenceCountedOpenSslContext.freeBio(j4);
                                ReferenceCountedOpenSslContext.freeBio(bio);
                                SSL.freePrivateKey(privateKey);
                                SSL.freeX509Chain(x509Chain);
                                throw th;
                            }
                        } else {
                            bio2 = 0;
                            privateKey = 0;
                        }
                        try {
                            keyMaterial = new CertificateRequestedCallback.KeyMaterial(x509Chain, privateKey);
                            ReferenceCountedOpenSslContext.freeBio(bio2);
                            ReferenceCountedOpenSslContext.freeBio(bio);
                        } catch (SSLException e6) {
                            e = e6;
                            throw e;
                        } catch (Exception e7) {
                            e = e7;
                            throw new SSLException(e);
                        } catch (Throwable th3) {
                            th = th3;
                            j4 = bio2;
                            ReferenceCountedOpenSslContext.freeBio(j4);
                            ReferenceCountedOpenSslContext.freeBio(bio);
                            SSL.freePrivateKey(privateKey);
                            SSL.freeX509Chain(x509Chain);
                            throw th;
                        }
                    } catch (SSLException e8) {
                        e = e8;
                        j3 = 0;
                        throw e;
                    } catch (Exception e9) {
                        e = e9;
                        j2 = 0;
                        throw new SSLException(e);
                    } catch (Throwable th4) {
                        th = th4;
                        x509Chain = 0;
                        privateKey = x509Chain;
                        ReferenceCountedOpenSslContext.freeBio(j4);
                        ReferenceCountedOpenSslContext.freeBio(bio);
                        SSL.freePrivateKey(privateKey);
                        SSL.freeX509Chain(x509Chain);
                        throw th;
                    }
                }
                SSL.freePrivateKey(0L);
                SSL.freeX509Chain(0L);
                return keyMaterial;
            } catch (Throwable th5) {
                th = th5;
            }
        } catch (SSLException e10) {
            e = e10;
            j3 = 0;
        } catch (Exception e11) {
            e = e11;
            j2 = 0;
        } catch (Throwable th6) {
            th = th6;
            bio = 0;
            x509Chain = 0;
        }
    }

    public void setKeyMaterial(ReferenceCountedOpenSslEngine referenceCountedOpenSslEngine) throws Throwable {
        String strChooseServerAlias;
        long jSslPointer = referenceCountedOpenSslEngine.sslPointer();
        String[] strArrAuthenticationMethods = SSL.authenticationMethods(jSslPointer);
        HashSet hashSet = new HashSet(strArrAuthenticationMethods.length);
        for (String str : strArrAuthenticationMethods) {
            String str2 = KEY_TYPES.get(str);
            if (str2 != null && (strChooseServerAlias = chooseServerAlias(referenceCountedOpenSslEngine, str2)) != null && hashSet.add(strChooseServerAlias)) {
                setKeyMaterial(jSslPointer, strChooseServerAlias);
            }
        }
    }
}
