【发布时间】: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 forCertificatePinner中描述的行。
标签: android ssl https retrofit okhttp3