【问题标题】:Implementing SSL pinning using Trustkit with Google Cloud Endpoints使用 Trustkit 和 Google Cloud Endpoints 实施 SSL 固定
【发布时间】:2018-07-31 21:52:57
【问题描述】:

我正在使用 Google Cloud Endpoints 与我的 Android 应用的应用引擎后端进行交互。我想实现公钥/SSL 固定。对于 Android N 及更高版本,这样做很容易,但我想为早期版本的 Android 实现固定。似乎一种常见的方法是使用Trustkit

Trustkit 链接上的入门说明,描述了如何通过设置 SSLSocketFactory 来进行设置,例如

  // HttpsUrlConnection
  HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
  connection.setSSLSocketFactory(TrustKit.getInstance().getSSLSocketFactory(serverHostname));

  // OkHttp 2.x
  OkHttpClient client =
    new OkHttpClient()
        .setSSLSocketFactory(TrustKit.getInstance().getSSLSocketFactory(serverHostname));

但由于连接的设置方式,我不确定如何将其应用于 Google Cloud Endpoints,例如,这是在我的代码中为 Google Cloud Endpoints 设置的方式

        import com.xxxx.backend.myApi.MyApi;
    
    //I believe the MyApi class is generated automatically as part of the Google Cloud Endpoints integration in Android Studio.
        
        class EndpointsAsyncTask  extends AsyncTask<HashMap, Void, String> {
        
        @Override
        protected String doInBackground(HashMap... params) {

        HashMap<String, String> dict = params[0];
        String data = dict.get("dataString");

        
         MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
                                .setRootUrl("https://xxxxx").setApplicationName("xxxx");
        
        myApiService = builder.build();
        
        //This calls the API method
        return myApiService.customApiMethodName(data).execute().getDict().toString();
        }
        }

我想知道如何在早期版本的 Android 中实现 SSL 固定,例如API16-API23 同时仍以我的应用程序如上所示的方式连接到 Google Cloud Endpoints?

如果可能的话,我想使用 Trustkit,也许有一种不同的方式可以设置如何与我的 Api 方法连接以便我可以使用它?如果这是不可能的,我可以使用 Trustkit 的替代品吗?或者只是一种完全不同的方式来实现 SSL 固定,同时仍然以这种方式使用 Google Cloud Endpoints?

【问题讨论】:

    标签: android google-app-engine google-cloud-endpoints pinning public-key-pinning


    【解决方案1】:

    您将不得不停止使用 AndroidHttp helper 并根据它编写自己的代码。

    您不能只调用new NetHttpTransport()new ApacheHttpTransport(),而是必须使用它们的构建器,例如:

    public static HttpTransport newCompatibleTransport(String hostname) {
      SSLSocketFactory factory = TrustKit.getInstance().getSSLSocketFactory(serverHostname);
      if (AndroidUtils.isMinimumSdkLevel(9)) {
        return new NetHttpTransport.Builder()
            .setSslSocketFactory(factory)
            .build();
      }
      return new ApacheHttpTransport.Builder()
          .setSslSocketFactory(factory)
          .build();
    }
    

    【讨论】:

    • 非常感谢,如果没有它,我将无法修复它。但是 ApacheHttpTransport 位对我不起作用。它不会让我将“工厂”设置为输入,因为它说它不兼容。所以我删除了 if 语句,只有 return new NetHttpTransport.Builder().setSslSocketFactory(factory).build();我还必须将工厂设置为实例变量,我认为这仅对我的特殊情况是必要的,因为我设置了所有内容的方式。但如果其他人遇到问题,他们也可以尝试。
    猜你喜欢
    • 2023-04-01
    • 2013-05-23
    • 2014-09-12
    • 2018-12-18
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2014-01-19
    • 2020-08-04
    相关资源
    最近更新 更多