【问题标题】:OkHttp3, Retrofit and certificate pinning: how to give an expiration to the pinningOkHttp3,改造和证书固定:如何使固定到期
【发布时间】:2020-02-12 00:24:21
【问题描述】:

在我的 Android 应用程序中,我需要使用证书固定。我正在使用 RetrofitOkHttp3 来使用 Web 服务,并且我已经定义了证书哈希码的固定。

CertificatePinner certificatePinner = new CertificatePinner.Builder()
                .add("dummy.com", "sha256/xxxxxxxxxx=")
                .build();     

OkHttpClient httpClient = new OkHttpClient.Builder()
        .certificatePinner(certificatePinner)
        .callTimeout(240, TimeUnit.SECONDS)
        .readTimeout(240, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
       .build();


Retrofit retrofitKripton = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(KriptonBinderConverterFactory.create())
        .addConverterFactory(ScalarsConverterFactory.create())
        .client(httpClient).build();

我想强制证书固定直到证书到期,之后我只想避免证书固定(这是因为我想避免该应用程序在证书到期后停止工作)。有没有办法告诉OkHpttp3/Retrofit 具有所需的行为?

提前致谢

【问题讨论】:

  • 你用的是哪个版本的Android SDK,改造?我正在尝试做同样的事情,但调试工作但在发布时看起来 okhttp/retrofit 有一些问题,但它没有记录问题。

标签: android ssl okhttp certificate-pinning


【解决方案1】:

有没有办法告诉 OkHpttp3/Retrofit 具有所需的行为?

你可以自己做:

OkHttpClient.Builder = new OkHttpClient.Builder();

if (applyPins()) {
    CertificatePinner certificatePinner = new CertificatePinner.Builder()
                    .add("dummy.com", "sha256/xxxxxxxxxx=")
                    .build();     

    builder..certificatePinner(certificatePinner);
}


OkHttpClient httpClient = builder
        .callTimeout(240, TimeUnit.SECONDS)
        .readTimeout(240, TimeUnit.SECONDS)
        .retryOnConnectionFailure(true)
       .build();

Retrofit retrofitKripton = new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addConverterFactory(KriptonBinderConverterFactory.create())
        .addConverterFactory(ScalarsConverterFactory.create())
        .client(httpClient).build();

实现applyPins()作为一个方法,如果你想应用引脚,则返回true,否则返回false。例如,您可以使用建议的日期比较。

【讨论】:

    【解决方案2】:

    根据以下增强功能,您正在寻找的功能在 OKHTTP 中仍然不可用。

    https://github.com/square/okhttp/issues/3010

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 2017-12-17
      • 2016-09-21
      • 2019-10-03
      • 2018-10-28
      • 2017-11-14
      • 2018-04-07
      • 1970-01-01
      • 2014-07-23
      相关资源
      最近更新 更多