Q&A for cert

本文探讨了如何在Android设备上安装及程序化访问X.509和PKCS#12证书,特别是在Android 4.0以下版本中访问私有证书的问题。文中详细介绍了使用反射调用`android.security.KeyStore.put()`方法来安装证书并指定别名的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Where is located private certificate store on Android version below 4.0?


Question1:

I'm developing Android app which read private certificate and key from Android key store. Certificate was imported on Android from .p12 file on SD CARD using Settings > security >Install from storage (Credetial storage).

On version 4.0 and higher I can get certificate and key from Android KeyChain. Can I programmatically get this certificate on Android version below 4.0?

I can see certificate and can choose it from WI-FI settings and that's all. I try "BKS" and "PKCS12" stores, but get empty resultset. I can access CA certificate store and get CA certificate data, but that is not what I need.

Answer1:

The key and certificate are encrypted and stored in /data/misc/keystore. However, since they have been stored by the system, you don't have the permission to access or decrypt them. Additionally, there is no public API for this.



How do you install a (x509 / pk12) certificate on ICS without users consent?

For an application I am developping, I need to be able to install CA and user certificates and private keys without his or hers concent.

I will have full system priviledges, and its fair to assume that the user will have a password before this happens. I will have the x509 if its a CA certificate or the pk12 if its a User certificate + private key file, and also the password if its the USer certificate + private key. I need to do this to be able to set up WPA-EAP wifi configurations automatically, and prefibly I want this to happen without the employees having to notice anything.

If anyone also know how to list all certificates that is installed, I would be very grateful.

I have checked throughout the day, and tested a bit with keystore_cli without success, and I have also read through the CertInstaller code without getting any wiser. Everything there is package-wide so I cannot call the methods directly, + it seems to send stuff further away to com.android.settings", "com.android.settings.CredentialStorage".

Any advice would be very great.

EDIT  For those wondering, here is how I did it with the CA Certificates. The application needs to be able to run as the system user (  android:sharedUserId="android.uid.system"  in android manifest ).

// Android...why do you enjoy doing my life so difficult...
            try {
                Class<?> keyStoreClass = WifiConfiguration.class.getClassLoader().loadClass("android.security.KeyStore");

                Method getInstanceMethod = keyStoreClass.getMethod("getInstance");
                Object keyStore = getInstanceMethod.invoke(null);

                Log.d("DeviceManager", "Got keystore" + keyStore.toString());

                // Put(Key, Value)
                Method putCertificateMethod = keyStoreClass.getMethod("put", String.class, byte[].class);

                Log.d("DeviceManager", "Putting...");

                RandomAccessFile file = new RandomAccessFile("/data/ca.crt", "r");
                byte[] b = new byte[(int)file.length()];
                file.read(b);
                byte[] cacert = b;

                Log.d("DeviceManager", "Certificate is bytes long: " + b.length);

                putCertificateMethod.invoke(keyStore, "CACERT_name", cacert);


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

Answer:

Fortunately this is not possible on a stock device. Otherwise, any rogue app will be able to install CA certificates without user consent. If you have small set of devices, you might have to pre-provision them. As for PKCS#12 files, they are password protected so someone will need to enter the password.

Not sure what you mean by 'full system privileges', but if you can link your app with platform code and sign it with the system certificate, you can call KeyChainService methods directly. This will let you install certificates. Additionally, CA certificates are just stored as files, so you can copy them over to the right place. Some details here: http://nelenkov.blogspot.jp/2011/11/ics-credential-storage-implementation.html

share | improve this answer


Hello, thank you for your answer and your brilliant articles. They helped me learn much more about the way this works. First of all. By saying full sysstem priviledges I do indeed mean that the app will be signed with the system certificate. This is a device management solution for a company producing their own hardware and software, giving me full control over the android eco system. I checked KeyChainService, and found the methodinstallCaCertificate(byte[] caCertificate) there, which might be relevant. But it seems to only take the certificate as bytes, not caring of the alias. [cont..] –  Aldrian  Jun 22 '12 at 7:56

I need to be able to assign aliases as well, as this will be used for automatically transfering wifi configurations to a users device, prefibly without them knowing or caring. Now, for Wifi and WPA-EAP, the server can require CA certificates, and/or private cert+keys, so I need to be able to install all of these things. For CA Certificates, they will be installed in /data/misc/keychain/cacerts-added and in /data/misc/keystore/. I have transfered ca certificates successfully to the cacerts-added folder, and they show up in the installed certificates, but not on the Wifi list. [cont...] –  Aldrian  Jun 22 '12 at 8:05

This seems to require it to come in the /data/misc/keystore folder as well, as i see 1000_CACERT_ca there, and it will pop up in the WPA EAP list as well. –  Aldrian  Jun 22 '12 at 8:06

Right, the WiFi code is not Java, so it doesn't use the cacerts-added/ files. You need to useandroid.security.KeyStore.put() to install the cert and give it an alias. Look atcom.android.settings.CredentialStorage for how to use it. Basically, you need to install using bothKeyChainService (for Java's TrustManager, etc) and KeyStore (for the WiFi and VPN infrastructure). –  Nikolay Elenkov  Jun 22 '12 at 8:21

First of all, thank you so much! I have been able to install a .crt CA certificate successfully using reflection on the android.security.KeyStore.put() method as the system user. That worked brilliantly. Then arises the question, how do I do the same with the pk12 certificate? I will know the password, so the user shouldn't need to type that in. –  Aldrian  Jun 22 '12 at 11:26



资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在 IT 领域,文档格式转换是常见需求,尤其在处理多种文件类型时。本文将聚焦于利用 Java 技术栈,尤其是 Apache POI 和 iTextPDF 库,实现 doc、xls(涵盖 Excel 2003 及 Excel 2007+)以及 txt、图片等格式文件向 PDF 的转换,并实现在线浏览功能。 先从 Apache POI 说起,它是一个强大的 Java 库,专注于处理 Microsoft Office 格式文件,比如 doc 和 xls。Apache POI 提供了 HSSF 和 XSSF 两个 API,其中 HSSF 用于读写老版本的 BIFF8 格式(Excel 97-2003),XSSF 则针对新的 XML 格式(Excel 2007+)。这两个 API 均具备读取和写入工作表、单元格、公式、样式等功能。读取 Excel 文件时,可通过创建 HSSFWorkbook 或 XSSFWorkbook 对象来打开相应格式的文件,进而遍历工作簿中的每个 Sheet,获取行和列数据。写入 Excel 文件时,创建新的 Workbook 对象,添加 Sheet、Row 和 Cell,即可构建新 Excel 文件。 再看 iTextPDF,它是一个用于生成和修改 PDF 文档的 Java 库,拥有丰富的 API。创建 PDF 文档时,借助 Document 对象,可定义页面尺寸、边距等属性来定制 PDF 外观。添加内容方面,可使用 Paragraph、List、Table 等元素将文本、列表和表格加入 PDF,图片可通过 Image 类加载插入。iTextPDF 支持多种字体和样式,可设置文本颜色、大小、样式等。此外,iTextPDF 的 TextRenderer 类能将 HTML、
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值