如何告诉Android Volley中的TLS版本

Ale*_*ang 8 ssl https android android-volley sslsocketfactory

我的项目已经使用Android Volley网络框架很长一段时间了,但最近我发现了Internet上发布的SSL 3.0协议错误.

我想知道如何找出我的项目使用的TLS版本,以及如何确认库是否更新.

这是我的源代码片段:

HttpStack stack = new HurlStack();
Network network = new BasicNetwork(stack);
mHttpRequestQueue = new RequestQueue(new NoCache(), network);
mHttpRequestQueue.start();
Run Code Online (Sandbox Code Playgroud)

我认为重点在于HurlStack类,它取决于org.apache.http包,但我无法弄清楚TLS/SSL配置的位置.

w3b*_*ark 18

您可以通过创建自定义HTTPStack并Volley.newRequestQueue(context, httpStack)Volley.java中的方法中设置堆栈来修改Volley中使用的TLS版本.虽然,您只需要为Android版本16-19执行此操作.在v16之前,不支持TLS 1.2,在v19之后,默认情况下启用TLS 1.2.因此,您应该专注于为Android版本16-19手动将TLS设置为1.2.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN
    && Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
    try {
      ProviderInstaller.installIfNeeded(getContext());
    } catch (GooglePlayServicesRepairableException e) {
      // Indicates that Google Play services is out of date, disabled, etc.
      // Prompt the user to install/update/enable Google Play services.
      GooglePlayServicesUtil.showErrorNotification(e.getConnectionStatusCode(), getContext());
      // Notify the SyncManager that a soft error occurred.
      syncResult.stats.numIOExceptions++;
      return;
    } catch (GooglePlayServicesNotAvailableException e) {
      // Indicates a non-recoverable error; the ProviderInstaller is not able
      // to install an up-to-date Provider.
      // Notify the SyncManager that a hard error occurred.
      syncResult.stats.numAuthExceptions++;
      return;
    }

    HttpStack stack = null;
    try {
      stack = new HurlStack(null, new TLSSocketFactory());
    } catch (KeyManagementException e) {
      e.printStackTrace();
      Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
      stack = new HurlStack();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      Log.d("Your Wrapper Class", "Could not create new stack for TLS v1.2");
      stack = new HurlStack();
    }
    requestQueue = Volley.newRequestQueue(context, stack);
} else {
  requestQueue = Volley.newRequestQueue(context);
}
Run Code Online (Sandbox Code Playgroud)

然后使用扩展SSLSocketFactory的TLSSocketFactory类,就像这里创建的Florian Krauthan一样,启用了v1.2 TLS协议:https://gist.github.com/fkrauthan/ac8624466a4dee4fd02f#file-tlssocketfactory-java

  • 我必须按照https://blog.dev-area.net/2015/08/17/protect-your-android-app-against-ssl-exploits/升级安全提供程序,然后才能在我的4.4设备上运行 (3认同)
  • @Ajji我已经更新了答案,包括检查以确保安全提供程序具有最新更新.对于其他人,您可以在此处阅读官方文档中的更多信息:https://developer.android.com/training/articles/security-gms-provider.html这可以同步完成(就像在我的回答)或异步.但是,它必须完成. (3认同)
  • @Diederik的观点非常重要.代码必须处理安全代码提供程序的更新.在我的情况下,即使安全提供程序已经被另一个应用程序更新,我仍然必须在我自己的应用程序中包含代码以使TLS工作. (2认同)