Android手动X509证书链验证

发布于 2024-12-18 18:36:46 字数 884 浏览 2 评论 0原文

我在代码中实现了 javax.net.ssl.X509TrustManager,以便我可以验证我的软件访问的自签名证书。但是,我仍然需要验证其他一些“标准”网站 SSL 证书。我正在使用 CertPathValidator.validate() 来做到这一点,但我刚刚意识到我正在传递的一个证书链(针对maps.googleapis.com)实际上并不包含完整的链 - 它包含整个链,但根 CA (Equifax) 确实存在于手机上,但 validate() 仍然失败,因为(显然)它没有明确地存在于链中。我在这里缺少什么才能使验证成功?感谢您的任何意见。

编辑 - 相关代码(删除了异常检查):

CertificateFactory cf = CertificateFactory.getInstance("X.509");
// chain is of type X509Certificate[]
CertPath cp = cf.generateCertPath(Arrays.asList(chain));

CertPathValidator cpv = CertPathValidator.getInstance(CertPathValidator.getDefaultType());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

FileInputStream is = new FileInputStream("/system/etc/security/cacerts.bks");
ks.load(is, null);

PKIXParameters params = new PKIXParameters(ks);
CertPathValidatorResult cpvr = cpv.validate(cp, params);

I have implemented javax.net.ssl.X509TrustManager in my code so I can validate my self-signed cert, which my software accesses. However, I still need to validate some other "standard" website SSL certificates. I am using CertPathValidator.validate() to do that, but I just realized that one cert chain I am being passed (for maps.googleapis.com) doesn't actually contain the complete chain - it contains the whole chain but the Root CA (Equifax), which does exist on the phone, but validate() still fails because (apparently) it's not explicitly in the chain. What am I missing here, so that the validation succeeds? Thanks for any input.

Edit - Relevant code (with exception checking removed):

CertificateFactory cf = CertificateFactory.getInstance("X.509");
// chain is of type X509Certificate[]
CertPath cp = cf.generateCertPath(Arrays.asList(chain));

CertPathValidator cpv = CertPathValidator.getInstance(CertPathValidator.getDefaultType());
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

FileInputStream is = new FileInputStream("/system/etc/security/cacerts.bks");
ks.load(is, null);

PKIXParameters params = new PKIXParameters(ks);
CertPathValidatorResult cpvr = cpv.validate(cp, params);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2024-12-25 18:36:46

实现您自己的 TrustManager 通常是一个坏主意。更好的方法是将证书添加到密钥库,并将其添加为信任库。有关如何执行此操作的示例,请参阅

您可能需要将默认信任存储添加到验证器中。另外,Android 默认情况下不进行吊销 (CRL) 检查,因此如果启用它,则需要手动获取相关的 CRL。发布您的代码。

Implementing your own TrustManager is generally a bad idea. The better way is to add your certificates to a keystore, and add it as a trust store. See this for an example of how to do it.

You probably need to add the default trust store to your validator. Also, Android does not do revocation (CRL) checking by default, so if you enabled it, you need to get the related CRL's manually. Post your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文