package com.mbridge.msdk.thrid.okhttp.internal.platform; import com.mbridge.msdk.thrid.okhttp.Protocol; import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.security.Provider; import java.util.List; import javax.annotation.Nullable; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.X509TrustManager; import org.conscrypt.Conscrypt; public class ConscryptPlatform extends Platform { private ConscryptPlatform() { } private Provider getProvider() { return Conscrypt.newProviderBuilder().provideTrustManager().build(); } @Override // com.mbridge.msdk.thrid.okhttp.internal.platform.Platform @Nullable public X509TrustManager trustManager(SSLSocketFactory sSLSocketFactory) { if (!Conscrypt.isConscrypt(sSLSocketFactory)) { return super.trustManager(sSLSocketFactory); } try { Object readFieldOrNull = readFieldOrNull(sSLSocketFactory, Object.class, "sslParameters"); if (readFieldOrNull != null) { return (X509TrustManager) readFieldOrNull(readFieldOrNull, X509TrustManager.class, "x509TrustManager"); } return null; } catch (Exception e) { throw new UnsupportedOperationException("clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on Conscrypt", e); } } @Override // com.mbridge.msdk.thrid.okhttp.internal.platform.Platform public void configureTlsExtensions(SSLSocket sSLSocket, String str, List<Protocol> list) throws IOException { if (Conscrypt.isConscrypt(sSLSocket)) { if (str != null) { Conscrypt.setUseSessionTickets(sSLSocket, true); Conscrypt.setHostname(sSLSocket, str); } Conscrypt.setApplicationProtocols(sSLSocket, (String[]) Platform.alpnProtocolNames(list).toArray(new String[0])); return; } super.configureTlsExtensions(sSLSocket, str, list); } @Override // com.mbridge.msdk.thrid.okhttp.internal.platform.Platform @Nullable public String getSelectedProtocol(SSLSocket sSLSocket) { if (Conscrypt.isConscrypt(sSLSocket)) { return Conscrypt.getApplicationProtocol(sSLSocket); } return super.getSelectedProtocol(sSLSocket); } @Override // com.mbridge.msdk.thrid.okhttp.internal.platform.Platform public SSLContext getSSLContext() { try { return SSLContext.getInstance("TLSv1.3", getProvider()); } catch (NoSuchAlgorithmException e) { try { return SSLContext.getInstance("TLS", getProvider()); } catch (NoSuchAlgorithmException unused) { throw new IllegalStateException("No TLS provider", e); } } } public static ConscryptPlatform buildIfSupported() { try { Class.forName("org.conscrypt.Conscrypt"); if (!Conscrypt.isAvailable()) { return null; } return new ConscryptPlatform(); } catch (ClassNotFoundException unused) { return null; } } @Override // com.mbridge.msdk.thrid.okhttp.internal.platform.Platform public void configureSslSocketFactory(SSLSocketFactory sSLSocketFactory) { if (Conscrypt.isConscrypt(sSLSocketFactory)) { Conscrypt.setUseEngineSocket(sSLSocketFactory, true); } } }