android - 使用 okhttp 找不到证书路径的信任 anchor

标签 android ssl https retrofit okhttp

所以我整晚都在努力让它工作,但似乎没有任何效果......我不断收到未找到证书路径的信任 anchor 。

这是我构建 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 -二进制| openssl enc -base64

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

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

如有任何帮助,我们将不胜感激!

最佳答案

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

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

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

关于android - 使用 okhttp 找不到证书路径的信任 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46165098/

相关文章:

java - CursorIndexOutOfBoundsException : Index -1 requested,,大小为 163

android - 如何在 Android 的先前 Activity 中接收数据

python - 如何找到自签名证书

linux - SSL 的透明代理问题

mysql - NSURLSession 密码安全

php - 图片不显示 https

javascript - JSON.stringify 忽略我的嵌套对象

android - 如何从服务中检测 youtube 的 MediaPlayer 事件

ssl - 设置让我们加密后网站重定向次数过多

javascript - Node.js https 获取请求 ECONNRESET