在我的 android 设备上,我手动安装了一个证书,目的是允许我访问特定网站。
当我查看安全 - 用户证书时,我看到可以看到我的证书。
我的网站显示为webview,所以我必须使用以下代码
@Override
public void onReceivedClientCertRequest(WebView view, final ClientCertRequest request) {
if (mCertificates == null || mPrivateKey == null) {
loadCertificateAndPrivateKey();
}
request.proceed(mPrivateKey, mCertificates);
}
Run Code Online (Sandbox Code Playgroud)
在我的 loadCertificateAndPrivateKey() 函数中,我要获得这样的证书
KeyStore ks = KeyStore.getInstance("AndroidCAStore");
if (ks != null)
{
ks.load(null, null);
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements())
{
String alias = (String) aliases.nextElement();
java.security.cert.X509Certificate cert = (java.security.cert.X509Certificate) ks.getCertificate(alias);
if(cert.getIssuerDN().getName().contains("TOTO")){
mCertificates = new X509Certificate[1];
mCertificates[0] = (X509Certificate)cert;
mPrivateKey = ??;
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何获取私钥......
我尝试过这种方式,但密钥对象为空(当我直接从我的应用程序加载我的证书时,相同的代码有效)
Key key = keyStore.getKey(alias, …Run Code Online (Sandbox Code Playgroud) 我有一个 Android 应用程序,可以在网络视图中调用安全网站。webview 检索证书并将其提供给网站。
我必须使用该KeyChain.choosePrivateKeyAlias(this, keyChainAliasCallback, null, null, null, -1, CERT_ALIAS);方法,这会显示一个像这样的对话框
我只想在用户第一次使用该应用程序时显示此窗口,但我不知道是否可行。
我看到关于使用设备/所有者配置文件拦截此内容。这是否意味着它应该在 android 上工作?对我来说有点模糊。
另一个解决方案是将证书和私钥保存在任何其他应用程序或用户都无法访问的地方。我考虑私有模式下的 SharedPreferences。
我错了吗 ?
感谢您的回答!