编程语言
首页 > 编程语言> > java-当generateCertificate()时出现CertificateException

java-当generateCertificate()时出现CertificateException

作者:互联网

我正在开发我的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(...)
      {...}
 ...
}

堆栈跟踪:

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)

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

附言:这是我从Android SDK X509Certificate,CertificateFactory开始使用的类

如果您对我为什么这样做感到好奇,原因是我希望我的应用程序能够通过以下代码安装证书(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);

解决方法:

您正在尝试读取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.isKeyEntry和KeyStore.isCertificateEntry方法确定条目类型.

根据您提供的KeyChain意图,您似乎想要在密钥链中添加新的受信任的Root 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);
}

标签:certificate,ssl-certificate,x509certificate,java,android
来源: https://codeday.me/bug/20191030/1966172.html