4

我正在开发我的安卓应用程序。我正在尝试从我的证​​书文件流中生成X509Certificate实例,但是得到CertificateException,这是我的简单代码:

import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
...
public class CertMgr {
   //my certification file 'mycert.p12' located in my app internal storage
   File certFile = GET_CERT();

   X509Certificate cert = null;
   try {

      FileInputStream fis = new FileInputStream(certFile);
      BufferedInputStream bis = new BufferedInputStream(fis);

      CertificateFactory cf = CertificateFactory.getInstance("X.509"); 

      if(bis.available() > 0){
           //I got CertificateException here, see the stack trace
          cert = (X509Certificate) cf.generateCertificate(bis); //line nr 150
      }
   }catch(...)
      {...}
 ...
}

堆栈跟踪:

javax.security.cert.CertificateException: org.apache.harmony.security.asn1.ASN1Exception: ASN.1 Sequence: mandatory value is missing at [4]
11-11 17:30:20.731: W/System.err(11529):    at javax.security.cert.X509Certificate.getInstance(X509Certificate.java:94)
11-11 17:30:20.731: W/System.err(11529):    at javax.security.cert.X509Certificate.getInstance(X509Certificate.java:213)
11-11 17:30:20.731: W/System.err(11529):    at com.my.app.CertMgr.getCert(CertMgr.java:150)

有人可以向我解释为什么我会得到这个例外吗?

PS:这是我从 Android SDK X509CertificateCertificateFactory使用的类

如果您对我这样做的原因感到好奇,原因是我希望我的应用程序能够通过以下代码安装证书( mycert.p12 )(如果上面的代码工作正常):

Intent installIntent = KeyChain.createInstallIntent();

installIntent.putExtra(KeyChain.EXTRA_CERTIFICATE, cert.getEncoded());
installIntent.putExtra(KeyChain.EXTRA_NAME, MY_CERT);
startActivityForResult(installIntent, INSTALL_KEYCHAIN_CODE);
4

1 回答 1

5

您正在尝试读取 PKCS#12 数据结构,因为它是 X509 证书。PKCS#12标准指定了一种数据结构,该结构可以捆绑多个证书和私钥,可选择由密码保护。

为了读取 PKCS#12 数据,您需要使用KeyStore加载它。此代码片段显示如何列出 PCKS#12 文件的所有条目:

KeyStore keyStore = KeyStore.getInstance("PKCS12");
File p12File = GET_CERT();
FileInputStream fis = new FileInputStream(p12File);
BufferedInputStream bis = new BufferedInputStream(fis);
keyStore.load(bis, password.toCharArray()); // password is the PKCS#12 password. If there is no password, just pass null
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
    String alias = aliases.nextElement();
    /* Do something with the keystore entry */
}

KeyStore 条目可以是私钥,有或没有相关的证书链(即从根证书到对应于私钥的证书的证书序列),或可信证书。KeyStore.isKeyEntry您可以通过和KeyStore.isCertificateEntry方法确定条目类型。

根据您提供的 KeyChain 意图,您似乎想在密钥链中添加一个新的受信任根 CA 证书。因此,我认为您应该列出 PKCS#12 文件的证书条目。

编辑(2013 年 11 月 12 日)

如何从密钥库中获取受信任的证书:

String alias = aliases.nextElement();
if (keyStore.isCertificateEntry(alias)) { // keep only trusted cert entries
    Certificate caCert = keyStore.getCertificate(alias)
    byte[] extraCertificate = caCert.getEncoded();
    Intent installIntent = KeyChain.createInstallIntent();
    installIntent.putExtra(KeyChain.EXTRA_CERTIFICATE, extraCertificate);
    installIntent.putExtra(KeyChain.EXTRA_NAME, MY_CERT);
    startActivityForResult(installIntent, INSTALL_KEYCHAIN_CODE);
}
于 2013-11-11T19:13:33.120 回答