package okhttp3.internal.platform;

import android.os.Build;
import c.h;
import c.q.d.g;
import c.q.d.j;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.cert.Certificate;
import java.security.cert.TrustAnchor;
import java.security.cert.X509Certificate;
import java.util.List;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.Protocol;
import okhttp3.internal.platform.Platform;
import okhttp3.internal.tls.CertificateChainCleaner;
import okhttp3.internal.tls.TrustRootIndex;

/* JADX INFO: compiled from: AndroidPlatform.kt */
/* JADX INFO: loaded from: classes.dex */
public final class AndroidPlatform extends Platform {
    public static final Companion Companion = new Companion(null);
    private static final int MAX_LOG_LENGTH = 4000;
    private final CloseGuard closeGuard;
    private final Method getAlpnSelectedProtocol;
    private final Method setAlpnProtocols;
    private final Method setHostname;
    private final Method setUseSessionTickets;
    private final Class<?> sslParametersClass;
    private final Class<?> sslSocketClass;

    /* JADX INFO: compiled from: AndroidPlatform.kt */
    public static final class AndroidCertificateChainCleaner extends CertificateChainCleaner {
        private final Method checkServerTrusted;
        private final Object x509TrustManagerExtensions;

        public AndroidCertificateChainCleaner(Object obj, Method method) {
            j.c(obj, "x509TrustManagerExtensions");
            j.c(method, "checkServerTrusted");
            this.x509TrustManagerExtensions = obj;
            this.checkServerTrusted = method;
        }

        @Override // okhttp3.internal.tls.CertificateChainCleaner
        public List<Certificate> clean(List<? extends Certificate> list, String str) throws SSLPeerUnverifiedException {
            j.c(list, "chain");
            j.c(str, "hostname");
            try {
                Object[] array = list.toArray(new X509Certificate[0]);
                if (array == null) {
                    throw new h("null cannot be cast to non-null type kotlin.Array<T>");
                }
                Object objInvoke = this.checkServerTrusted.invoke(this.x509TrustManagerExtensions, (X509Certificate[]) array, "RSA", str);
                if (objInvoke != null) {
                    return (List) objInvoke;
                }
                throw new h("null cannot be cast to non-null type kotlin.collections.List<java.security.cert.Certificate>");
            } catch (IllegalAccessException e2) {
                throw new AssertionError(e2);
            } catch (InvocationTargetException e3) {
                SSLPeerUnverifiedException sSLPeerUnverifiedException = new SSLPeerUnverifiedException(e3.getMessage());
                sSLPeerUnverifiedException.initCause(e3);
                throw sSLPeerUnverifiedException;
            }
        }

        public boolean equals(Object obj) {
            return obj instanceof AndroidCertificateChainCleaner;
        }

        public int hashCode() {
            return 0;
        }
    }

    /* JADX INFO: compiled from: AndroidPlatform.kt */
    public static final class CloseGuard {
        public static final Companion Companion = new Companion(null);
        private final Method getMethod;
        private final Method openMethod;
        private final Method warnIfOpenMethod;

        /* JADX INFO: compiled from: AndroidPlatform.kt */
        public static final class Companion {
            private Companion() {
            }

            public final CloseGuard get() throws NoSuchMethodException {
                Method method;
                Method method2;
                Method method3 = null;
                try {
                    Class<?> cls = Class.forName("dalvik.system.CloseGuard");
                    Method method4 = cls.getMethod("get", new Class[0]);
                    method2 = cls.getMethod("open", String.class);
                    method = cls.getMethod("warnIfOpen", new Class[0]);
                    method3 = method4;
                } catch (Exception unused) {
                    method = null;
                    method2 = null;
                }
                return new CloseGuard(method3, method2, method);
            }

            public /* synthetic */ Companion(g gVar) {
                this();
            }
        }

        public CloseGuard(Method method, Method method2, Method method3) {
            this.getMethod = method;
            this.openMethod = method2;
            this.warnIfOpenMethod = method3;
        }

        public final Object createAndOpen(String str) {
            j.c(str, "closer");
            Method method = this.getMethod;
            if (method != null) {
                try {
                    Object objInvoke = method.invoke(null, new Object[0]);
                    Method method2 = this.openMethod;
                    if (method2 != null) {
                        method2.invoke(objInvoke, str);
                        return objInvoke;
                    }
                    j.g();
                    throw null;
                } catch (Exception unused) {
                }
            }
            return null;
        }

        public final boolean warnIfOpen(Object obj) {
            if (obj == null) {
                return false;
            }
            try {
                Method method = this.warnIfOpenMethod;
                if (method != null) {
                    method.invoke(obj, new Object[0]);
                    return true;
                }
                j.g();
                throw null;
            } catch (Exception unused) {
                return false;
            }
        }
    }

    /* JADX INFO: compiled from: AndroidPlatform.kt */
    public static final class Companion {
        private Companion() {
        }

        public final Platform buildIfSupported() {
            try {
                Class<?> cls = Class.forName("com.android.org.conscrypt.SSLParametersImpl");
                j.b(cls, "Class.forName(\"com.andro…crypt.SSLParametersImpl\")");
                Class<?> cls2 = Class.forName("com.android.org.conscrypt.OpenSSLSocketImpl");
                j.b(cls2, "Class.forName(\"com.andro…crypt.OpenSSLSocketImpl\")");
                if (Build.VERSION.SDK_INT >= 21) {
                    try {
                        Method declaredMethod = cls2.getDeclaredMethod("setUseSessionTickets", Boolean.TYPE);
                        Method method = cls2.getMethod("setHostname", String.class);
                        Method method2 = cls2.getMethod("getAlpnSelectedProtocol", new Class[0]);
                        Method method3 = cls2.getMethod("setAlpnProtocols", byte[].class);
                        j.b(declaredMethod, "setUseSessionTickets");
                        j.b(method, "setHostname");
                        j.b(method2, "getAlpnSelectedProtocol");
                        j.b(method3, "setAlpnProtocols");
                        return new AndroidPlatform(cls, cls2, declaredMethod, method, method2, method3);
                    } catch (NoSuchMethodException unused) {
                    }
                }
                throw new IllegalStateException("Expected Android API level 21+ but was " + Build.VERSION.SDK_INT);
            } catch (ClassNotFoundException unused2) {
                return null;
            }
        }

        public /* synthetic */ Companion(g gVar) {
            this();
        }
    }

    /* JADX INFO: compiled from: AndroidPlatform.kt */
    public static final class CustomTrustRootIndex implements TrustRootIndex {
        private final Method findByIssuerAndSignatureMethod;
        private final X509TrustManager trustManager;

        public CustomTrustRootIndex(X509TrustManager x509TrustManager, Method method) {
            j.c(x509TrustManager, "trustManager");
            j.c(method, "findByIssuerAndSignatureMethod");
            this.trustManager = x509TrustManager;
            this.findByIssuerAndSignatureMethod = method;
        }

        private final X509TrustManager component1() {
            return this.trustManager;
        }

        private final Method component2() {
            return this.findByIssuerAndSignatureMethod;
        }

        public static /* synthetic */ CustomTrustRootIndex copy$default(CustomTrustRootIndex customTrustRootIndex, X509TrustManager x509TrustManager, Method method, int i, Object obj) {
            if ((i & 1) != 0) {
                x509TrustManager = customTrustRootIndex.trustManager;
            }
            if ((i & 2) != 0) {
                method = customTrustRootIndex.findByIssuerAndSignatureMethod;
            }
            return customTrustRootIndex.copy(x509TrustManager, method);
        }

        public final CustomTrustRootIndex copy(X509TrustManager x509TrustManager, Method method) {
            j.c(x509TrustManager, "trustManager");
            j.c(method, "findByIssuerAndSignatureMethod");
            return new CustomTrustRootIndex(x509TrustManager, method);
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof CustomTrustRootIndex)) {
                return false;
            }
            CustomTrustRootIndex customTrustRootIndex = (CustomTrustRootIndex) obj;
            return j.a(this.trustManager, customTrustRootIndex.trustManager) && j.a(this.findByIssuerAndSignatureMethod, customTrustRootIndex.findByIssuerAndSignatureMethod);
        }

        @Override // okhttp3.internal.tls.TrustRootIndex
        public X509Certificate findByIssuerAndSignature(X509Certificate x509Certificate) {
            j.c(x509Certificate, "cert");
            try {
                Object objInvoke = this.findByIssuerAndSignatureMethod.invoke(this.trustManager, x509Certificate);
                if (objInvoke != null) {
                    return ((TrustAnchor) objInvoke).getTrustedCert();
                }
                throw new h("null cannot be cast to non-null type java.security.cert.TrustAnchor");
            } catch (IllegalAccessException e2) {
                throw new AssertionError("unable to get issues and signature", e2);
            } catch (InvocationTargetException unused) {
                return null;
            }
        }

        public int hashCode() {
            X509TrustManager x509TrustManager = this.trustManager;
            int iHashCode = (x509TrustManager != null ? x509TrustManager.hashCode() : 0) * 31;
            Method method = this.findByIssuerAndSignatureMethod;
            return iHashCode + (method != null ? method.hashCode() : 0);
        }

        public String toString() {
            return "CustomTrustRootIndex(trustManager=" + this.trustManager + ", findByIssuerAndSignatureMethod=" + this.findByIssuerAndSignatureMethod + ")";
        }
    }

    public AndroidPlatform(Class<?> cls, Class<?> cls2, Method method, Method method2, Method method3, Method method4) {
        j.c(cls, "sslParametersClass");
        j.c(cls2, "sslSocketClass");
        j.c(method, "setUseSessionTickets");
        j.c(method2, "setHostname");
        j.c(method3, "getAlpnSelectedProtocol");
        j.c(method4, "setAlpnProtocols");
        this.sslParametersClass = cls;
        this.sslSocketClass = cls2;
        this.setUseSessionTickets = method;
        this.setHostname = method2;
        this.getAlpnSelectedProtocol = method3;
        this.setAlpnProtocols = method4;
        this.closeGuard = CloseGuard.Companion.get();
    }

    private final boolean api23IsCleartextTrafficPermitted(String str, Class<?> cls, Object obj) throws IllegalAccessException, InvocationTargetException {
        try {
            Object objInvoke = cls.getMethod("isCleartextTrafficPermitted", new Class[0]).invoke(obj, new Object[0]);
            if (objInvoke != null) {
                return ((Boolean) objInvoke).booleanValue();
            }
            throw new h("null cannot be cast to non-null type kotlin.Boolean");
        } catch (NoSuchMethodException unused) {
            return super.isCleartextTrafficPermitted(str);
        }
    }

    private final boolean api24IsCleartextTrafficPermitted(String str, Class<?> cls, Object obj) throws IllegalAccessException, InvocationTargetException {
        try {
            Object objInvoke = cls.getMethod("isCleartextTrafficPermitted", String.class).invoke(obj, str);
            if (objInvoke != null) {
                return ((Boolean) objInvoke).booleanValue();
            }
            throw new h("null cannot be cast to non-null type kotlin.Boolean");
        } catch (NoSuchMethodException unused) {
            return api23IsCleartextTrafficPermitted(str, cls, obj);
        }
    }

    @Override // okhttp3.internal.platform.Platform
    public CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager x509TrustManager) {
        j.c(x509TrustManager, "trustManager");
        try {
            Class<?> cls = Class.forName("android.net.http.X509TrustManagerExtensions");
            Object objNewInstance = cls.getConstructor(X509TrustManager.class).newInstance(x509TrustManager);
            Method method = cls.getMethod("checkServerTrusted", X509Certificate[].class, String.class, String.class);
            j.b(objNewInstance, "extensions");
            j.b(method, "checkServerTrusted");
            return new AndroidCertificateChainCleaner(objNewInstance, method);
        } catch (Exception unused) {
            return super.buildCertificateChainCleaner(x509TrustManager);
        }
    }

    @Override // okhttp3.internal.platform.Platform
    public TrustRootIndex buildTrustRootIndex(X509TrustManager x509TrustManager) {
        j.c(x509TrustManager, "trustManager");
        try {
            Method declaredMethod = x509TrustManager.getClass().getDeclaredMethod("findTrustAnchorByIssuerAndSignature", X509Certificate.class);
            j.b(declaredMethod, "method");
            declaredMethod.setAccessible(true);
            return new CustomTrustRootIndex(x509TrustManager, declaredMethod);
        } catch (NoSuchMethodException unused) {
            return super.buildTrustRootIndex(x509TrustManager);
        }
    }

    @Override // okhttp3.internal.platform.Platform
    public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<? extends Protocol> list) {
        j.c(sSLSocket, "sslSocket");
        j.c(list, "protocols");
        if (this.sslSocketClass.isInstance(sSLSocket)) {
            if (str != null) {
                try {
                    this.setUseSessionTickets.invoke(sSLSocket, Boolean.TRUE);
                    this.setHostname.invoke(sSLSocket, str);
                } catch (IllegalAccessException e2) {
                    throw new AssertionError(e2);
                } catch (InvocationTargetException e3) {
                    throw new AssertionError(e3);
                }
            }
            this.setAlpnProtocols.invoke(sSLSocket, Platform.Companion.concatLengthPrefixed(list));
        }
    }

    @Override // okhttp3.internal.platform.Platform
    public void connectSocket(Socket socket, InetSocketAddress inetSocketAddress, int i) throws IOException {
        j.c(socket, "socket");
        j.c(inetSocketAddress, "address");
        try {
            socket.connect(inetSocketAddress, i);
        } catch (ClassCastException e2) {
            if (Build.VERSION.SDK_INT != 26) {
                throw e2;
            }
            throw new IOException("Exception in connect", e2);
        }
    }

    @Override // okhttp3.internal.platform.Platform
    public String getSelectedProtocol(SSLSocket sSLSocket) {
        j.c(sSLSocket, "socket");
        if (!this.sslSocketClass.isInstance(sSLSocket)) {
            return null;
        }
        try {
            byte[] bArr = (byte[]) this.getAlpnSelectedProtocol.invoke(sSLSocket, new Object[0]);
            if (bArr == null) {
                return null;
            }
            Charset charset = StandardCharsets.UTF_8;
            j.b(charset, "UTF_8");
            return new String(bArr, charset);
        } catch (IllegalAccessException e2) {
            throw new AssertionError(e2);
        } catch (InvocationTargetException e3) {
            throw new AssertionError(e3);
        }
    }

    @Override // okhttp3.internal.platform.Platform
    public Object getStackTraceForCloseable(String str) {
        j.c(str, "closer");
        return this.closeGuard.createAndOpen(str);
    }

    @Override // okhttp3.internal.platform.Platform
    public boolean isCleartextTrafficPermitted(String str) {
        j.c(str, "hostname");
        try {
            Class<?> cls = Class.forName("android.security.NetworkSecurityPolicy");
            Object objInvoke = cls.getMethod("getInstance", new Class[0]).invoke(null, new Object[0]);
            j.b(cls, "networkPolicyClass");
            j.b(objInvoke, "networkSecurityPolicy");
            return api24IsCleartextTrafficPermitted(str, cls, objInvoke);
        } catch (ClassNotFoundException unused) {
            return super.isCleartextTrafficPermitted(str);
        } catch (IllegalAccessException e2) {
            throw new AssertionError("unable to determine cleartext support", e2);
        } catch (IllegalArgumentException e3) {
            throw new AssertionError("unable to determine cleartext support", e3);
        } catch (NoSuchMethodException unused2) {
            return super.isCleartextTrafficPermitted(str);
        } catch (InvocationTargetException e4) {
            throw new AssertionError("unable to determine cleartext support", e4);
        }
    }

    /* JADX WARN: Code restructure failed: missing block: B:18:0x0053, code lost:
    
        r8 = r2 + 1;
     */
    @Override // okhttp3.internal.platform.Platform
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void log(int r8, java.lang.String r9, java.lang.Throwable r10) {
        /*
            r7 = this;
            java.lang.String r0 = "message"
            c.q.d.j.c(r9, r0)
            r0 = 5
            if (r8 != r0) goto L9
            goto La
        L9:
            r0 = 3
        La:
            if (r10 == 0) goto L24
            java.lang.StringBuilder r8 = new java.lang.StringBuilder
            r8.<init>()
            r8.append(r9)
            java.lang.String r9 = "\n"
            r8.append(r9)
            java.lang.String r9 = android.util.Log.getStackTraceString(r10)
            r8.append(r9)
            java.lang.String r9 = r8.toString()
        L24:
            r8 = 0
            int r10 = r9.length()
        L29:
            if (r8 >= r10) goto L60
            r2 = 10
            r4 = 0
            r5 = 4
            r6 = 0
            r1 = r9
            r3 = r8
            int r1 = c.u.f.G(r1, r2, r3, r4, r5, r6)
            r2 = -1
            if (r1 == r2) goto L3a
            goto L3b
        L3a:
            r1 = r10
        L3b:
            int r2 = r8 + 4000
            int r2 = java.lang.Math.min(r1, r2)
            if (r9 == 0) goto L58
            java.lang.String r8 = r9.substring(r8, r2)
            java.lang.String r3 = "(this as java.lang.Strin…ing(startIndex, endIndex)"
            c.q.d.j.b(r8, r3)
            java.lang.String r3 = "OkHttp"
            android.util.Log.println(r0, r3, r8)
            if (r2 < r1) goto L56
            int r8 = r2 + 1
            goto L29
        L56:
            r8 = r2
            goto L3b
        L58:
            c.h r8 = new c.h
            java.lang.String r9 = "null cannot be cast to non-null type java.lang.String"
            r8.<init>(r9)
            throw r8
        L60:
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: okhttp3.internal.platform.AndroidPlatform.log(int, java.lang.String, java.lang.Throwable):void");
    }

    @Override // okhttp3.internal.platform.Platform
    public void logCloseableLeak(String str, Object obj) {
        j.c(str, "message");
        if (this.closeGuard.warnIfOpen(obj)) {
            return;
        }
        log(5, str, null);
    }

    @Override // okhttp3.internal.platform.Platform
    protected X509TrustManager trustManager(SSLSocketFactory sSLSocketFactory) throws IllegalAccessException {
        j.c(sSLSocketFactory, "sslSocketFactory");
        Object fieldOrNull = Platform.Companion.readFieldOrNull(sSLSocketFactory, this.sslParametersClass, "sslParameters");
        if (fieldOrNull == null) {
            try {
                Class<?> cls = Class.forName("com.google.android.gms.org.conscrypt.SSLParametersImpl", false, sSLSocketFactory.getClass().getClassLoader());
                Platform.Companion companion = Platform.Companion;
                j.b(cls, "gmsSslParametersClass");
                fieldOrNull = companion.readFieldOrNull(sSLSocketFactory, cls, "sslParameters");
            } catch (ClassNotFoundException unused) {
                return super.trustManager(sSLSocketFactory);
            }
        }
        Platform.Companion companion2 = Platform.Companion;
        if (fieldOrNull != null) {
            X509TrustManager x509TrustManager = (X509TrustManager) companion2.readFieldOrNull(fieldOrNull, X509TrustManager.class, "x509TrustManager");
            return x509TrustManager != null ? x509TrustManager : (X509TrustManager) Platform.Companion.readFieldOrNull(fieldOrNull, X509TrustManager.class, "trustManager");
        }
        j.g();
        throw null;
    }
}
