当 generateCertificate() 时出现 CertificateException

Lee*_*fin 4 java android certificate ssl-certificate x509certificate

我正在开发我的 android 应用程序。我正在尝试从我的证书文件流中生成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(...)
      {...}
 ...
}
Run Code Online (Sandbox Code Playgroud)

堆栈跟踪:

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)
Run Code Online (Sandbox Code Playgroud)

有人可以向我解释为什么我会收到此异常吗?

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);
Run Code Online (Sandbox Code Playgroud)

Jcs*_*Jcs 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 */
}
Run Code Online (Sandbox Code Playgroud)

KeyStore 条目可以是私钥,带有或不带有关联的证书链(即从根证书到与私钥对应的证书的证书序列),或受信任的证书。您可以通过KeyStore.isKeyEntryKeyStore.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);
}
Run Code Online (Sandbox Code Playgroud)