【问题标题】:Trust anchor for certification path not found with okhttp使用 okhttp 找不到证书路径的信任锚
【发布时间】:2018-02-20 06:00:08
【问题描述】:

所以我整晚都在努力让它发挥作用,但似乎没有任何效果......我不断得到 Trust anchor for certificate path not found。

这是我构建 okhttpClient 的方式(我关注了https://medium.com/@sreekumar_av/certificate-public-key-pinning-in-android-using-retrofit-2-0-74140800025b

fun provideOkHttpClient(): OkHttpClient {
    val httpClientBuilder = OkHttpClient()
            .newBuilder()

    val logging = HttpLoggingInterceptor()
    logging.level = if (BuildConfig.DEBUG)
        HttpLoggingInterceptor.Level.BODY
    else
        HttpLoggingInterceptor.Level.NONE


    val certificatePinner = CertificatePinner.Builder()
            .add(HOST, SHA)
            .build()

    val connectionSpec = ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
    connectionSpec.tlsVersions(TlsVersion.TLS_1_2).build()

    val tlsSocketFactory = TLSSocketFactory()
    return httpClientBuilder
            .certificatePinner(certificatePinner)
            .addNetworkInterceptor(logging)
            .sslSocketFactory(tlsSocketFactory, tlsSocketFactory.systemDefaultTrustManager())
            .connectionSpecs(Collections.singletonList(connectionSpec.build()))
            .build()
}

我得到了这样的 sha:openssl s_client -connect host:port | openssl x509 -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64

这里是 TLSFacotry:https://gist.github.com/pollux-/fbcc74984e110bb49497faa2d0ed5ee1#file-tlssocketfactory-java

我真的不明白为什么它在那个时候不起作用..

任何帮助将不胜感激!

【问题讨论】:

  • IIRC,完整的 LogCat 输出将显示 OkHttp 遇到的情况,因此您可以将其与 certificatePinner 中的内容进行比较。我不知道你为什么要打sslSocketFactory() 电话。
  • 我没有关于 logcat 的更多信息,但这里是完整的日志:gist.github.com/NeoDigi/60abbcdd1f650897f600afda9268837b
  • 它不会在堆栈跟踪中,而是在它之前或之后的行中。
  • 没什么了,也许有拦截器什么的,我可以添加更多信息,但截至目前,真的没什么了
  • httpClientBuilder 中删除除certificatePinner() 之外的所有内容,然后重试。您正在寻找“证书固定失败!” the JavaDocs for CertificatePinner 中描述的行。

标签: android ssl https retrofit okhttp3


【解决方案1】:

IIRC 证书固定仅在证书链被接受后评估。

拥有完整的堆栈跟踪以及您尝试连接的主机会有所帮助,因为它可能使用您的客户端不接受的自签名证书。

先尝试在没有证书固定的情况下使其工作,然后将其添加。正如@CommonsWare 提到的,当证书固定失败时,它会告诉您该怎么做。

【讨论】:

    猜你喜欢
    • 2019-06-18
    • 2014-01-29
    • 2017-10-03
    • 2019-05-19
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多