我有一个简单的web api在覆盆子pi上运行,它位于同一个pi上的nginx服务器后面。我使用自签名客户端证书来验证来自Android应用程序的调用。这在过去完全没问题,但我最近在重建一些硬件之后回到了这个项目,当我尝试在运行Android 8.1的Pixel 2上使用它时,它提供了以下例外:
javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
at com.android.org.conscrypt.ConscryptFileDescriptorSocket.startHandshake(ConscryptFileDescriptorSocket.java:219)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:268)
...
我根据http://nategood.com/client-side-certificate-authentication-in-ngi
生成了证书和密钥使用curl
进行测试可以正常工作。
我为应用创建了密钥库,用于:
openssl pkcs12 -export -clcerts -in client.crt -inkey client.key -out client.p12
我按照以下文章在Android中设置客户端证书并连接到服务器:http://chariotsolutions.com/blog/post/https-with-client-certificates-on/
但我在Kotlin中用OkHttp重写了它:
private const val SERVER = "https://my.server"
/**
* trustManagers is used to authorize the server's self-signed cert
*/
private val trustManagers by lazy {
val cert = CertificateFactory.getInstance("X.509")
.generateCertificate(appCtx.assets.open("ca.crt")) as X509Certificate
val trustStore = KeyStore.getInstance(KeyStore.getDefaultType()).apply {
load(null, null)
setCertificateEntry(cert.subjectX500Principal.name, cert)
}
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()).apply {
init(trustStore)
}.trustManagers
}
/**
* keyManagers is used to load the client-authentication cert
*/
private val keyManagers by lazy {
// assuming this can only be called after Application is created
val keyStore = KeyStore.getInstance("PKCS12").apply {
load(appCtx.assets.open("client.p12"), "".toCharArray())
}
KeyManagerFactory.getInstance("X509").apply {
init(keyStore, "".toCharArray())
}.keyManagers
}
/**
* sslContext for opening TLS connection to server
*/
private val sslContext by lazy {
SSLContext.getInstance("TLS").apply {
init(keyManagers, trustManagers, null)
}
}
/**
* pass an HTTPS request to server
*/
suspend fun request(url: String): ByteArray? {
return try {
val request = Request.Builder()
.url(url)
.build()
val client = OkHttpClient.Builder()
.sslSocketFactory(sslContext.socketFactory, trustManagers[0] as X509TrustManager)
.build()
client.newCall(request).await().body().byteStream().readBytes()
} catch (e: Exception) {
err(e) { "failed to send request" }
null
}
}
这个使用工作,但现在却没有。我花了一天半的时间寻找答案,我尝试了以下方法:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<!-- Additionally trust user added CAs -->
<certificates src="user" />
<certificates src="@raw/ca"/>
</trust-anchors>
</base-config>
</network-security-config>
我已经阅读了我在创建自定义信任管理器时可以找到的所有示例,它们几乎完全相同,甚至https://developer.android.com/training/articles/security-ssl.html#UnknownCa
我试过的Everythign会产生同样的异常,我错过了什么吗?
答案 0 :(得分:0)
这就是我使用 GoDaddy 上的自签名证书让它在我的项目中工作的方式:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<debug-overrides>
<trust-anchors>
<certificates src="user"/>
<certificates src="@raw/gdcert"/>
</trust-anchors>
</debug-overrides>
</network-security-config>
android:networkSecurityConfig="@xml/network_security_config"
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory ssf = sc.getSocketFactory();
SSLSocket sslsocket = (SSLSocket) ssf.createSocket("yourwebsitename.com", 443);
之后是您尝试运行以连接到服务器的任何代码。
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}};
保存您的项目并运行它。