我有一个 PKCS#12我将其视为 keystore 文件,因为它包含一个 key 条目和一个证书条目。
在 Android 中,我看到人们通过以下方式以编程方式安装 keystore (代码来自 Android developer blog):
byte[] keystore = . . (read from a PKCS#12 keystore)
Intent installIntent = KeyChain.createInstallIntent();
installIntent.putExtra(KeyChain.EXTRA_PKCS12, keystore);
startActivityForResult(installIntent, INSTALL_KEYSTORE_CODE);
我还看到人们以编程方式仅安装证书包裹在 keystore 中:
Intent intent = KeyChain.createInstallIntent();
intent.putExtra(KeyChain.EXTRA_CERTIFICATE, cert);
startActivity(intent);
此外,我还看到有人同时安装 keystore 和包装在 keystore 中的证书。例如,this article向我们展示了如何首先安装 keystore ,然后以编程方式安装包装在 keystore 中的证书。
我真的很困惑什么时候应该只安装 keystore ,什么时候应该只安装证书(包装在 keystore 中)?我应该什么时候安装两者?有人可以让我清楚这一点吗?
例如,我的 keystore PKCS#12 文件 (mycert.p12) 包含 key /证书对,用于连接到 VPN 服务器。我的 android 客户端应该什么时候安装包含在 keystore 中的 keystore 和证书?客户端应该什么时候安装包含在 keystore 中的唯一证书?有什么区别 ?我对此很困惑。
I have a PKCS#12 file which I considered as a keystore file since it contains one key entry & one certificate entry.
正确。
In Android, I see people programmatically install keystore in the following way ...
这是在您拥有 keystore (即 key 对和证书)时完成的。
I also see people programmatically install only the certificate wrapped inside keystore
这是在您拥有某人的其他证书(通常是自签名证书)时完成的,该证书不受任何已安装的默认 CA(证书颁发机构)的信任。您永远不必这样做。
因此请注意,您永远不会使用同一个证书同时执行这两项操作,因为情况(所有权)不同。毫无疑问,哪个过程是合适的。如果是您的,请导入 keystore 。如果是别人的,导入证书。
所有这些东西的最终规范引用是 ITU Recommendation X.509 .
最后,关于您所链接的质量较差的博客文章的一些说明。
来自 Unifying key store access in ICS :
In the past, it was common practice for apps to maintain their own key
store if they needed to authenticate a secure SSL web server, or
authenticate the user to a server via a client certificate.
这已经不正确了。
要验证 Web 服务器,您不需要任何东西,如果它有 CA 签名的证书。如果它有自签名证书,您需要将其导入您的truststore。
要向 Web 服务器验证您自己的身份,您需要一个包含您自己的私钥和证书的 keystore ,最好是 CA 签名的证书。否则,服务器必须将您的自签名证书导入其信任库,即上述 (1) 的逆过程。不要走这条路。自签名证书带来的麻烦远远超过它们的值(value),这没什么,从您为它们支付的价格就可以看出这一点。
来自 Using ICS keychain API :
We first get the private key and certificate chain using the key alias
and then create and verify a signature to check if the key is actually
usable.
完全是胡说八道。我们已经有了私钥、公钥和证书。它们已经可以使用了。创建签名并在本地验证它完全是在浪费时间。
Installing a CA certificate is not very different from installing a
PKCS#12 file: you load the certificate in a byte array and pass it as
an extra to the install intent.
区别在于您在 CA 证书案例中使用 KeyChain.EXTRA_CERTIFICATE
,在 keystore 案例中使用 KeyChain.EXTRA_PKCS12
。