【问题标题】:Android 4.4 Volley connection error to nginxAndroid 4.4 Volley 连接到 Nginx 错误
【发布时间】:2019-02-19 20:11:56
【问题描述】:

我无法让 API

我尝试按照其他地方的建议使用VolleyToolboxExtensionNoSSLv3Factory,但仍然遇到如下所示的硬错误,除了 sslv3 而不是 tlsv1。

我目前的方法是让旧的 android 版本通过 SSLv3 连接。但这仍然会产生如下错误。

注意:我知道 SSLv3 已损坏,但我使用的是不同的证书,并且我没有发送任何与安全相关的内容。整个想法是进行一项调查,以便我可以决定是否要放弃对旧 android SDK 版本的支持。

error during request: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb8a45890: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x8d959990:0x00000000)

我正在使用以下代码进行连接:

final String url;
if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP ){
    url = "https://abc.badssl.example.com";
} else {
    url = "https://abc.example.com";
}
final JSONObject jsonBody;
try {
    jsonBody = new JSONObject("{\"api\":" + Build.VERSION.SDK_INT + "}");
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
            jsonBody,
            response -> {
                try {
                    Log.d(TAG, "Response is: " + response.getInt("api"));
                } catch (JSONException e) {
                    Log.wtf(TAG, e);
                }
                wasRunning = true;
                loading.postValue(false);
            }, e -> {
        Log.e(TAG, "error during request: " + e.getMessage());
        error.postValue(true);
    });
    jsonObjectRequest.setShouldCache(false);
    jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.getCache().clear();

    queue.add(jsonObjectRequest);
} catch (JSONException e) {
    Log.wtf(TAG, e);
}

这是我对 abc.badssl.example.com 的 nginx 配置,使用不同的证书:

listen  443 ssl;
server_name abc.badssl.example.com;

ssl on;
ssl_protocols SSLv3;
ssl_dhparam /etc/nginx/ssl/dhparams.pem;

#worker shared ssl cache
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_ciphers 'ECDHE-ECDSA-AES256-SHA:EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:!DHE-RSA-AES256-GCM-SHA384:!DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:!DHE-RSA-AES256-SHA256:!DHE-RSA-AES128-SHA256:!DHE-RSA-AES256-SHA:!DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4:!DHE-RSA-DES-CBC-SHA:!DHE-RSA-CAMELLIA256-SHA:!DHE-RSA-CAMELLIA128-SHA:!DHE-DSS-CBC-SHA:!DHE-RSA-DES-CBC3-SHA';

fastcgi_param HTTPS on;

我认为我的 nginx 参数有误,但我似乎找不到 99% 是关于禁用 SSLv3 或与当前版本的 volley 无关(SO 中的死链接)。

【问题讨论】:

    标签: android nginx https android-volley


    【解决方案1】:

    好的,所以解决方案有点不同。

    首先:出于显而易见的原因,在当前版本的 OpenSSL 中,在 debian 和 ubuntu 上禁用了 SSLv3。有趣的是,这不会引发任何错误等,nginx 配置协议会被忽略?!即使配置中只有 SSLv3,我仍然可以通过 TLS1.X 连接到服务器。 nmap 报告相同。

    在 Android

    在旧设备上获得有效的 HTTPSUrlConnection 最简单的解决方案是使用 NetCipher。

    build.gradle:

    implementation "info.guardianproject.netcipher:netcipher:2.0.0-alpha1"
    

    然后使用 Netcipher 创建 HTTPs 连接。
    您还可以通过使用自定义 HttpStack 将其与 volley 一起使用。请注意,Netcipher 的StrongBuilders 不用于建立简单的https 连接!它们仅用于通过Tor 网络建立连接。所以不要误以为你可以使用这些来避免 HttpStack。

    没有 volley 的请求处理程序,使用原始 HttpsUrlConnection,剥离:

    final String url = "https://asd.example.com";
    runner = new Thread(() -> {
    
        final JSONObject jsonBody;
        try {
            HttpsURLConnection con = NetCipher.getHttpsURLConnection(new URL(url));
            con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            con.setRequestProperty("Accept", "application/json");
            con.setDoOutput(true);
            con.setDoInput(true);
    
            jsonBody = new JSONObject("{\"api\":" + Build.VERSION.SDK_INT + "}");
    
            OutputStream wr = con.getOutputStream();
            wr.write(jsonBody.toString().getBytes("UTF-8"));
            wr.flush();
            wr.close();
    
            InputStream in = new BufferedInputStream(con.getInputStream());
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }
    
            JSONObject jsonObject = new JSONObject(result.toString("UTF-8"));
            in.close();
    
    
            con.disconnect();
            Log.d(TAG,"received json: "+jsonObject.toString());
            loading.postValue(false);
        } catch (
                Exception e) {
            Log.wtf(TAG, e);
        }
    });
    runner.start();
    

    【讨论】:

      猜你喜欢
      • 2018-07-31
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 2015-04-24
      • 2018-12-17
      • 2016-05-07
      相关资源
      最近更新 更多