我遇到与以下4个设备连接到服务器的异常的android 4设备有关的问题:
java.security.cert.CertPathValidatorException:的信任锚
找不到认证路径。
在com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:410)
在okhttp3.internal.connection.RealConnection.connectTls(SourceFile:319)
在okhttp3.internal.connection.RealConnection。EstablishmentProtocol(SourceFile:283)
在okhttp3.internal.connection.RealConnection.connect(SourceFile:168)
在okhttp3.internal.connection.StreamAllocation.findConnection(SourceFile:257)
在okhttp3.internal.connection.StreamAllocation.findHealthyConnection(SourceFile:135)
在okhttp3.internal.connection.StreamAllocation.newStream(SourceFile:114)
在okhttp3.internal.connection.ConnectInterceptor.intercept(SourceFile:42)
在okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
在okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
在okhttp3.internal.cache.CacheInterceptor.intercept(SourceFile:93)
在okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
在okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
在okhttp3.internal.http.BridgeInterceptor.intercept(SourceFile:93)
在okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
在okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(SourceFile:126)
在okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:147)
在okhttp3.internal.http.RealInterceptorChain.proceed(SourceFile:121)
在okhttp3.RealCall.getResponseWithInterceptorChain(SourceFile:254)
在okhttp3.RealCall.execute(SourceFile:92)
服务器证书来自Cloudflare,我使用https://www.digicert.com/help/等几种工具进行了检查,看来还可以。
但是由于某种原因,我不知道它在Android 4版本中开始失败。
尝试了信任所有证书[LINK]的解决方案,并且可以正常工作,但这显然存在安全问题,例如将您的应用程序暴露给“中间人”攻击
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
okHttpBuilder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
如何实现具有默认行为的TrustManager,但仅将服务器证书列入白名单。
谢谢
编辑:遵循OkHttp@CustomTrust上的示例(感谢CommonsWare)
使用了以下命令:
openssl s_client -showcerts -servername www.serverdomain.com -connect www.serverdomain.com:443
在证书链上,我给了我两种格式的证书:
-----BEGIN CERTIFICATE-----
....
-----END CERTIFICATE-----
将示例中的url和证书替换为获得的url和证书,但是仍然无法正常工作,有什么想法吗?
最佳答案
您需要将证书存储到原始文件夹中,然后:
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream instream = context.getResources().openRawResource(R.raw.gtux_cert);
Certificate ca;
try {
ca = cf.generateCertificate(instream);
} finally {
caInput.close();
}
KeyStore kStore = KeyStore.getInstance(KeyStore.getDefaultType());
kStore.load(null, null);
kStore.setCertificateEntry("ca", ca);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm(););
tmf.init(kStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
okHttpClient.setSslSocketFactory(context.getSocketFactory());
此处更多信息:Security SSL