package okhttp3.internal.platform; import android.os.Build; import android.util.Log; import com.vungle.ads.internal.presenter.MRAIDPresenter; 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.StandardCharsets; import java.security.NoSuchAlgorithmException; import java.security.cert.TrustAnchor; import java.security.cert.X509Certificate; import java.util.List; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLPeerUnverifiedException; import javax.net.ssl.SSLSocket; import javax.net.ssl.X509TrustManager; import okhttp3.internal.Util; import okhttp3.internal.tls.CertificateChainCleaner; import okhttp3.internal.tls.TrustRootIndex; /* loaded from: classes5.dex */ public class AndroidPlatform extends Platform { public final CloseGuard closeGuard = CloseGuard.get(); public final Method getAlpnSelectedProtocol; public final Method setAlpnProtocols; public final Method setHostname; public final Method setUseSessionTickets; public final Class sslParametersClass; public final Class sslSocketClass; public static int getSdkInt() { try { return Build.VERSION.SDK_INT; } catch (NoClassDefFoundError unused) { return 0; } } public AndroidPlatform(Class cls, Class cls2, Method method, Method method2, Method method3, Method method4) { this.sslParametersClass = cls; this.sslSocketClass = cls2; this.setUseSessionTickets = method; this.setHostname = method2; this.getAlpnSelectedProtocol = method3; this.setAlpnProtocols = method4; } @Override // okhttp3.internal.platform.Platform public void connectSocket(Socket socket, InetSocketAddress inetSocketAddress, int i) { try { socket.connect(inetSocketAddress, i); } catch (AssertionError e) { if (!Util.isAndroidGetsocknameError(e)) { throw e; } throw new IOException(e); } catch (ClassCastException e2) { if (Build.VERSION.SDK_INT == 26) { throw new IOException("Exception in connect", e2); } throw e2; } } @Override // okhttp3.internal.platform.Platform public void configureTlsExtensions(SSLSocket sSLSocket, String str, List list) { if (this.sslSocketClass.isInstance(sSLSocket)) { if (str != null) { try { this.setUseSessionTickets.invoke(sSLSocket, Boolean.TRUE); this.setHostname.invoke(sSLSocket, str); } catch (IllegalAccessException e) { e = e; throw new AssertionError(e); } catch (InvocationTargetException e2) { e = e2; throw new AssertionError(e); } } this.setAlpnProtocols.invoke(sSLSocket, Platform.concatLengthPrefixed(list)); } } @Override // okhttp3.internal.platform.Platform public String getSelectedProtocol(SSLSocket sSLSocket) { if (!this.sslSocketClass.isInstance(sSLSocket)) { return null; } try { byte[] bArr = (byte[]) this.getAlpnSelectedProtocol.invoke(sSLSocket, new Object[0]); if (bArr != null) { return new String(bArr, StandardCharsets.UTF_8); } return null; } catch (IllegalAccessException | InvocationTargetException e) { throw new AssertionError(e); } } @Override // okhttp3.internal.platform.Platform public void log(int i, String str, Throwable th) { int min; int i2 = i != 5 ? 3 : 5; if (th != null) { str = str + '\n' + Log.getStackTraceString(th); } int length = str.length(); int i3 = 0; while (i3 < length) { int indexOf = str.indexOf(10, i3); if (indexOf == -1) { indexOf = length; } while (true) { min = Math.min(indexOf, i3 + 4000); Log.println(i2, "OkHttp", str.substring(i3, min)); if (min >= indexOf) { break; } else { i3 = min; } } i3 = min + 1; } } @Override // okhttp3.internal.platform.Platform public Object getStackTraceForCloseable(String str) { return this.closeGuard.createAndOpen(str); } @Override // okhttp3.internal.platform.Platform public void logCloseableLeak(String str, Object obj) { if (this.closeGuard.warnIfOpen(obj)) { return; } log(5, str, null); } @Override // okhttp3.internal.platform.Platform public boolean isCleartextTrafficPermitted(String str) { try { Class cls = Class.forName("android.security.NetworkSecurityPolicy"); return api24IsCleartextTrafficPermitted(str, cls, cls.getMethod("getInstance", new Class[0]).invoke(null, new Object[0])); } catch (ClassNotFoundException | NoSuchMethodException unused) { return super.isCleartextTrafficPermitted(str); } catch (IllegalAccessException e) { e = e; throw new AssertionError("unable to determine cleartext support", e); } catch (IllegalArgumentException e2) { e = e2; throw new AssertionError("unable to determine cleartext support", e); } catch (InvocationTargetException e3) { e = e3; throw new AssertionError("unable to determine cleartext support", e); } } public final boolean api24IsCleartextTrafficPermitted(String str, Class cls, Object obj) { try { return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", String.class).invoke(obj, str)).booleanValue(); } catch (NoSuchMethodException unused) { return api23IsCleartextTrafficPermitted(str, cls, obj); } } public final boolean api23IsCleartextTrafficPermitted(String str, Class cls, Object obj) { try { return ((Boolean) cls.getMethod("isCleartextTrafficPermitted", new Class[0]).invoke(obj, new Object[0])).booleanValue(); } catch (NoSuchMethodException unused) { return super.isCleartextTrafficPermitted(str); } } @Override // okhttp3.internal.platform.Platform public CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager x509TrustManager) { try { Class cls = Class.forName("android.net.http.X509TrustManagerExtensions"); return new AndroidCertificateChainCleaner(cls.getConstructor(X509TrustManager.class).newInstance(x509TrustManager), cls.getMethod("checkServerTrusted", X509Certificate[].class, String.class, String.class)); } catch (Exception unused) { return super.buildCertificateChainCleaner(x509TrustManager); } } public static Platform buildIfSupported() { if (!Platform.isAndroid()) { return null; } try { Class cls = Class.forName("com.android.org.conscrypt.SSLParametersImpl"); Class cls2 = Class.forName("com.android.org.conscrypt.OpenSSLSocketImpl"); try { return new AndroidPlatform(cls, cls2, cls2.getDeclaredMethod("setUseSessionTickets", Boolean.TYPE), cls2.getMethod("setHostname", String.class), cls2.getMethod("getAlpnSelectedProtocol", new Class[0]), cls2.getMethod("setAlpnProtocols", byte[].class)); } catch (NoSuchMethodException unused) { throw new IllegalStateException("Expected Android API level 21+ but was " + Build.VERSION.SDK_INT); } } catch (ClassNotFoundException unused2) { return null; } } @Override // okhttp3.internal.platform.Platform public TrustRootIndex buildTrustRootIndex(X509TrustManager x509TrustManager) { try { Method declaredMethod = x509TrustManager.getClass().getDeclaredMethod("findTrustAnchorByIssuerAndSignature", X509Certificate.class); declaredMethod.setAccessible(true); return new CustomTrustRootIndex(x509TrustManager, declaredMethod); } catch (NoSuchMethodException unused) { return super.buildTrustRootIndex(x509TrustManager); } } public static final class AndroidCertificateChainCleaner extends CertificateChainCleaner { public final Method checkServerTrusted; public final Object x509TrustManagerExtensions; public int hashCode() { return 0; } public AndroidCertificateChainCleaner(Object obj, Method method) { this.x509TrustManagerExtensions = obj; this.checkServerTrusted = method; } @Override // okhttp3.internal.tls.CertificateChainCleaner public List clean(List list, String str) { try { return (List) this.checkServerTrusted.invoke(this.x509TrustManagerExtensions, (X509Certificate[]) list.toArray(new X509Certificate[list.size()]), "RSA", str); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InvocationTargetException e2) { SSLPeerUnverifiedException sSLPeerUnverifiedException = new SSLPeerUnverifiedException(e2.getMessage()); sSLPeerUnverifiedException.initCause(e2); throw sSLPeerUnverifiedException; } } public boolean equals(Object obj) { return obj instanceof AndroidCertificateChainCleaner; } } public static final class CloseGuard { public final Method getMethod; public final Method openMethod; public final Method warnIfOpenMethod; public CloseGuard(Method method, Method method2, Method method3) { this.getMethod = method; this.openMethod = method2; this.warnIfOpenMethod = method3; } public Object createAndOpen(String str) { Method method = this.getMethod; if (method != null) { try { Object invoke = method.invoke(null, new Object[0]); this.openMethod.invoke(invoke, str); return invoke; } catch (Exception unused) { } } return null; } public boolean warnIfOpen(Object obj) { if (obj == null) { return false; } try { this.warnIfOpenMethod.invoke(obj, new Object[0]); return true; } catch (Exception unused) { return false; } } public static CloseGuard get() { Method method; Method method2; Method method3; try { Class cls = Class.forName("dalvik.system.CloseGuard"); method = cls.getMethod("get", new Class[0]); method3 = cls.getMethod(MRAIDPresenter.OPEN, String.class); method2 = cls.getMethod("warnIfOpen", new Class[0]); } catch (Exception unused) { method = null; method2 = null; method3 = null; } return new CloseGuard(method, method3, method2); } } public static final class CustomTrustRootIndex implements TrustRootIndex { public final Method findByIssuerAndSignatureMethod; public final X509TrustManager trustManager; public CustomTrustRootIndex(X509TrustManager x509TrustManager, Method method) { this.findByIssuerAndSignatureMethod = method; this.trustManager = x509TrustManager; } @Override // okhttp3.internal.tls.TrustRootIndex public X509Certificate findByIssuerAndSignature(X509Certificate x509Certificate) { try { TrustAnchor trustAnchor = (TrustAnchor) this.findByIssuerAndSignatureMethod.invoke(this.trustManager, x509Certificate); if (trustAnchor != null) { return trustAnchor.getTrustedCert(); } return null; } catch (IllegalAccessException e) { throw new AssertionError("unable to get issues and signature", e); } catch (InvocationTargetException unused) { return null; } } public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof CustomTrustRootIndex)) { return false; } CustomTrustRootIndex customTrustRootIndex = (CustomTrustRootIndex) obj; return this.trustManager.equals(customTrustRootIndex.trustManager) && this.findByIssuerAndSignatureMethod.equals(customTrustRootIndex.findByIssuerAndSignatureMethod); } public int hashCode() { return this.trustManager.hashCode() + (this.findByIssuerAndSignatureMethod.hashCode() * 31); } } @Override // okhttp3.internal.platform.Platform public SSLContext getSSLContext() { try { return SSLContext.getInstance("TLS"); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("No TLS provider", e); } } }