无法在Android 4.4.4上建立TLS版本请求

时间:2018-02-19 14:32:18

标签: android android-volley android-security

以下是Volley的代码:

class VolleyService {
    public RequestQueue volley(Context context) {
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {

            try {
                ProviderInstaller.installIfNeeded(context);
            } catch (GooglePlayServicesRepairableException e) {
                Log.e("Test321", "PlayServices not installed");
            } catch (GooglePlayServicesNotAvailableException e) {
                Log.e("Test321", "Google Play Services not available.");
            }

            HttpStack stack = null;
            try {
                stack = new HurlStack(null, new TLSSocketFactory());  // WORKS FINE TILL HERE
            } catch (KeyManagementException | NoSuchAlgorithmException e) {
                e.printStackTrace();
                stack = new HurlStack();
            }
            return Volley.newRequestQueue(context, stack);
        } else {
            return Volley.newRequestQueue(context);
        }
    }

    private class TLSSocketFactory extends SSLSocketFactory {

        private SSLSocketFactory internalSSLSocketFactory;

        TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, null, null);
            internalSSLSocketFactory = context.getSocketFactory();
        }

        @Override
        public String[] getDefaultCipherSuites() {
            return internalSSLSocketFactory.getDefaultCipherSuites();
        }

        @Override
        public String[] getSupportedCipherSuites() {
            return internalSSLSocketFactory.getSupportedCipherSuites();
        }

        @Override
        public Socket createSocket() throws IOException {
            return enableTLSOnSocket(internalSSLSocketFactory.createSocket());
        }

        @Override
        public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
            return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
        }

        @Override
        public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
            return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
        }

        @Override
        public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
            return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
        }

        @Override
        public Socket createSocket(InetAddress host, int port) throws IOException {
            return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
        }

        @Override
        public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
            return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
        }

        private Socket enableTLSOnSocket(Socket socket) {
            if (socket != null && (socket instanceof SSLSocket)) {
                ((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.1", "TLSv1.2"});
            }
            return socket;
        }
    }
}

它调用TLSSocketFactory class然后没有结果。 有人可以帮我解决这个问题吗?

请不要参考。

1 个答案:

答案 0 :(得分:0)

您的服务器上似乎只配置了新的TLS版本支持。某些Android 4.4.4设备不支持TLS 1.2: https://github.com/ssllabs/ssllabs-scan/issues/258

您需要在网络服务器上启用旧版TLS(例如TLS 1.1)的支持。