如何在卸载应用程序时删除应用程序安装的受信任 CA 证书

How to remove App installed trusted CA cert on uninstalling the App(如何在卸载应用程序时删除应用程序安装的受信任 CA 证书)

本文介绍了如何在卸载应用程序时删除应用程序安装的受信任 CA 证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序提供安装 CA 证书的选项,它存储在 Trusted Credentials 的用户选项卡中,并且按预期工作.

I have an app that gives option to install CA cert and it gets stored in the user tab of Trusted Credentials and it works as expected.

仅供参考 (这是我安装证书的方式):

Intent installIntent = KeyChain.createInstallIntent();
javax.security.cert.X509Certificate x509 = javax.security.cert.X509Certificate.getInstance(caRootCertBytes);
installIntent.putExtra(KeyChain.EXTRA_CERTIFICATE, x509.getEncoded());
installIntent.putExtra(KeyChain.EXTRA_NAME,caRootCertName);
startActivity(installIntent);

如果应用已卸载,则证书仍保留在受信任的凭据中.

If the app is uninstalled the cert remains in the Trusted credentials.

我希望在卸载应用程序时卸载证书.

I would like the cert to be uninstalled when the application is uninstalled.

我想过使用 删除证书KeyStore 的 deleteEntry 方法.

I thought of removing the cert using deleteEntry method of KeyStore.

仅供参考 (虽然我还没有测试过.希望它应该可以工作.我会在测试后更新)

javax.security.cert.X509Certificate x509 = javax.security.cert.X509Certificate.getInstance(caRootCertBytes);

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);
                                String name = x509.getIssuerDN().getName();                             
                                if (cert.getIssuerDN().getName().contains(name)) 
                                {
                                  ks. deleteEntry(alias)

                                }
                            }
                        }  

即使您认为上述代码有效,我也无法注册广播接收器以卸载我自己的应用程序.

Even though if you consider above code works AFAIK I can't register broadcast receiver for uninstallation of my own app.

我如何才能在卸载我的应用时删除我的应用安装的证书?

感谢任何帮助!

推荐答案

你无法为你自己的包获得卸载包的广播.这可能会导致系统不一致.看到这个答案

you cant get the broadcast of package getting uninstalled for your own package. this may lead to inconsistency in the system. see this answer

这篇关于如何在卸载应用程序时删除应用程序安装的受信任 CA 证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在卸载应用程序时删除应用程序安装的受信任 CA 证书

基础教程推荐