Android X509TrustManager#checkServerTrusted 在 API > 23 上抛出 CertificateException
Android X509TrustManager#checkServerTrusted throws CertificateException on API > 23
我正在尝试将自签名证书导入受信任的证书,以便默认浏览器接受使用它的网站连接。
使用 https://github.com/bitfireAT/cadroid 中的代码归结为通过 X509TrustManager#checkServerTrusted()
进行的检查,该检查适用于 API 23 但不适用于 API 24/25(由 targetSdkVersion
).
public boolean isTrusted() throws NoSuchAlgorithmException, KeyStoreException {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore) null);
X509TrustManager tm = (X509TrustManager) tmf.getTrustManagers()[0];
try {
tm.checkServerTrusted(certificates, certificates[0].getPublicKey().getAlgorithm());
return true;
} catch (CertificateException e) {
return false;
}
}
证书已成功导入并受信任(由浏览器验证/它显示在系统设置 > 安全 > CA 证书中)因此它不应抛出异常(并且不会在 API 23 ).
我发现 Android 7.0 的唯一变化是没有提及任何相关内容 (https://developer.android.com/about/versions/nougat/android-7.0-changes.html#tls-ssl)。
任何想法,如何检查 API > 23 上的证书是否受信任?
我刚刚找到 解决了我的问题。
By default, secure connections (using protocols like TLS and HTTPS) from all apps trust the pre-installed system CAs, and apps targeting Android 6.0 (API level 23) and lower also trust the user-added CA store by default.
所以我需要在清单中包含 networkSecurityConfig
并允许用户证书。
我正在尝试将自签名证书导入受信任的证书,以便默认浏览器接受使用它的网站连接。
使用 https://github.com/bitfireAT/cadroid 中的代码归结为通过 X509TrustManager#checkServerTrusted()
进行的检查,该检查适用于 API 23 但不适用于 API 24/25(由 targetSdkVersion
).
public boolean isTrusted() throws NoSuchAlgorithmException, KeyStoreException {
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init((KeyStore) null);
X509TrustManager tm = (X509TrustManager) tmf.getTrustManagers()[0];
try {
tm.checkServerTrusted(certificates, certificates[0].getPublicKey().getAlgorithm());
return true;
} catch (CertificateException e) {
return false;
}
}
证书已成功导入并受信任(由浏览器验证/它显示在系统设置 > 安全 > CA 证书中)因此它不应抛出异常(并且不会在 API 23 ).
我发现 Android 7.0 的唯一变化是没有提及任何相关内容 (https://developer.android.com/about/versions/nougat/android-7.0-changes.html#tls-ssl)。
任何想法,如何检查 API > 23 上的证书是否受信任?
我刚刚找到
By default, secure connections (using protocols like TLS and HTTPS) from all apps trust the pre-installed system CAs, and apps targeting Android 6.0 (API level 23) and lower also trust the user-added CA store by default.
所以我需要在清单中包含 networkSecurityConfig
并允许用户证书。