翰林优商.apk(点击下载) / CustomSSLSocketFactory.java


package com.czhj.sdk.common.network;

import android.net.SSLCertificateSocketFactory;
import android.os.Build;
import com.czhj.sdk.common.utils.Preconditions;
import com.czhj.sdk.common.utils.ReflectionUtil;
import com.czhj.sdk.logger.SigmobLog;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class CustomSSLSocketFactory extends SSLSocketFactory {
    private static final TrustManager[] b = {new X509TrustManager() {
        /* class com.czhj.sdk.common.network.CustomSSLSocketFactory.AnonymousClass1 */

        @Override // javax.net.ssl.X509TrustManager
        public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) {
        }

        @Override // javax.net.ssl.X509TrustManager
        public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    }};
    private SSLSocketFactory a;

    private CustomSSLSocketFactory() {
    }

    private static void a(SSLCertificateSocketFactory sSLCertificateSocketFactory, SSLSocket sSLSocket, String str) {
        Preconditions.NoThrow.checkNotNull(sSLCertificateSocketFactory);
        Preconditions.NoThrow.checkNotNull(sSLSocket);
        if (Build.VERSION.SDK_INT >= 17) {
            sSLCertificateSocketFactory.setHostname(sSLSocket, str);
            return;
        }
        try {
            new ReflectionUtil.MethodBuilder(sSLSocket, "setHostname").addParam(String.class, str).execute();
        } catch (Throwable unused) {
            SigmobLog.d("Unable to call setHostname() on the socket");
        }
    }

    private void a(Socket socket) {
        if (socket instanceof SSLSocket) {
            SSLSocket sSLSocket = (SSLSocket) socket;
            sSLSocket.setEnabledProtocols(sSLSocket.getSupportedProtocols());
        }
    }

    private void a(Socket socket, String str) throws IOException {
        Preconditions.NoThrow.checkNotNull(socket);
        SSLSocketFactory sSLSocketFactory = this.a;
        if (sSLSocketFactory == null) {
            throw new SocketException("SSLSocketFactory was null. Unable to create socket.");
        } else if (socket instanceof SSLSocket) {
            try {
                SSLSocket sSLSocket = (SSLSocket) socket;
                if (sSLSocketFactory instanceof SSLCertificateSocketFactory) {
                    a((SSLCertificateSocketFactory) sSLSocketFactory, sSLSocket, str);
                    a(sSLSocket, str);
                }
            } catch (Throwable th) {
                SigmobLog.e(th.getMessage());
            }
        }
    }

    private static void a(SSLSocket sSLSocket, String str) throws IOException {
        Preconditions.NoThrow.checkNotNull(sSLSocket);
        sSLSocket.startHandshake();
        if (!HttpsURLConnection.getDefaultHostnameVerifier().verify(str, sSLSocket.getSession())) {
            throw new SSLHandshakeException("Server Name Identification failed.");
        }
    }

    public static CustomSSLSocketFactory getDefault(int i) {
        CustomSSLSocketFactory customSSLSocketFactory = new CustomSSLSocketFactory();
        customSSLSocketFactory.a = SSLCertificateSocketFactory.getDefault(i, null);
        return customSSLSocketFactory;
    }

    /* JADX WARNING: Removed duplicated region for block: B:15:0x002d A[RETURN] */
    /* JADX WARNING: Removed duplicated region for block: B:16:0x002e  */
    public static CustomSSLSocketFactory getForceValid() {
        SSLContext sSLContext;
        String str;
        KeyManagementException e;
        NoSuchAlgorithmException e2;
        CustomSSLSocketFactory customSSLSocketFactory = new CustomSSLSocketFactory();
        try {
            sSLContext = SSLContext.getInstance("SSL");
            try {
                sSLContext.init(null, b, new SecureRandom());
            } catch (KeyManagementException e3) {
                e = e3;
            } catch (NoSuchAlgorithmException e4) {
                e2 = e4;
                str = e2.getMessage();
                SigmobLog.e(str);
                if (sSLContext != null) {
                }
            }
        } catch (KeyManagementException e5) {
            e = e5;
            sSLContext = null;
            str = e.getMessage();
            SigmobLog.e(str);
            if (sSLContext != null) {
            }
        } catch (NoSuchAlgorithmException e6) {
            e2 = e6;
            sSLContext = null;
            str = e2.getMessage();
            SigmobLog.e(str);
            if (sSLContext != null) {
            }
        }
        if (sSLContext != null) {
            return null;
        }
        customSSLSocketFactory.a = sSLContext.getSocketFactory();
        return customSSLSocketFactory;
    }

    /* access modifiers changed from: package-private */
    @Deprecated
    public void a(SSLSocketFactory sSLSocketFactory) {
        this.a = sSLSocketFactory;
    }

    @Override // javax.net.SocketFactory
    public Socket createSocket() throws IOException {
        SSLSocketFactory sSLSocketFactory = this.a;
        if (sSLSocketFactory != null) {
            Socket createSocket = sSLSocketFactory.createSocket();
            a(createSocket);
            return createSocket;
        }
        throw new SocketException("SSLSocketFactory was null. Unable to create socket.");
    }

    @Override // javax.net.SocketFactory
    public Socket createSocket(String str, int i) throws IOException {
        SSLSocketFactory sSLSocketFactory = this.a;
        if (sSLSocketFactory != null) {
            Socket createSocket = sSLSocketFactory.createSocket(str, i);
            a(createSocket);
            return createSocket;
        }
        throw new SocketException("SSLSocketFactory was null. Unable to create socket.");
    }

    @Override // javax.net.SocketFactory
    public Socket createSocket(String str, int i, InetAddress inetAddress, int i2) throws IOException {
        SSLSocketFactory sSLSocketFactory = this.a;
        if (sSLSocketFactory != null) {
            Socket createSocket = sSLSocketFactory.createSocket(str, i, inetAddress, i2);
            a(createSocket);
            return createSocket;
        }
        throw new SocketException("SSLSocketFactory was null. Unable to create socket.");
    }

    @Override // javax.net.SocketFactory
    public Socket createSocket(InetAddress inetAddress, int i) throws IOException {
        SSLSocketFactory sSLSocketFactory = this.a;
        if (sSLSocketFactory != null) {
            Socket createSocket = sSLSocketFactory.createSocket(inetAddress, i);
            a(createSocket);
            return createSocket;
        }
        throw new SocketException("SSLSocketFactory was null. Unable to create socket.");
    }

    @Override // javax.net.SocketFactory
    public Socket createSocket(InetAddress inetAddress, int i, InetAddress inetAddress2, int i2) throws IOException {
        SSLSocketFactory sSLSocketFactory = this.a;
        if (sSLSocketFactory != null) {
            Socket createSocket = sSLSocketFactory.createSocket(inetAddress, i, inetAddress2, i2);
            a(createSocket);
            return createSocket;
        }
        throw new SocketException("SSLSocketFactory was null. Unable to create socket.");
    }

    @Override // javax.net.ssl.SSLSocketFactory
    public Socket createSocket(Socket socket, String str, int i, boolean z) throws IOException {
        if (this.a == null) {
            throw new SocketException("SSLSocketFactory was null. Unable to create socket.");
        } else if (Build.VERSION.SDK_INT < 23) {
            if (z && socket != null) {
                socket.close();
            }
            Socket createSocket = this.a.createSocket(InetAddressUtils.getInetAddressByName(str), i);
            a(createSocket);
            a(createSocket, str);
            return createSocket;
        } else {
            Socket createSocket2 = this.a.createSocket(socket, str, i, z);
            a(createSocket2);
            return createSocket2;
        }
    }

    public String[] getDefaultCipherSuites() {
        SSLSocketFactory sSLSocketFactory = this.a;
        return sSLSocketFactory == null ? new String[0] : sSLSocketFactory.getDefaultCipherSuites();
    }

    public String[] getSupportedCipherSuites() {
        SSLSocketFactory sSLSocketFactory = this.a;
        return sSLSocketFactory == null ? new String[0] : sSLSocketFactory.getSupportedCipherSuites();
    }
}