【问题标题】:Certification chain contains only one position - Java & PKCS11认证链只包含一个位置——Java & PKCS11
【发布时间】:2020-11-24 06:21:30
【问题描述】:

我正在尝试使用我的智能卡签署我的文件。我像这样初始化我的密钥库:

String pkcs11config = "name = CertumSmartCard \n" + "library = "
            + new File(".").getAbsolutePath() + "/cryptoCertum3PKCS.dll";
Provider pkcs11Provider = new SunPKCS11(new ByteArrayInputStream(
                pkcs11config.getBytes()));
Security.addProvider(pkcs11Provider);
KeyStore keyStore = KeyStore.getInstance("PKCS11", pkcs11Provider);
keyStore.load(null, pin.toCharArray());

然后我尝试使用以下方法读取证书链:

Enumeration<String> aliasesEnum = keyStore.aliases();
String alias = null;
while (aliasesEnum.hasMoreElements()) {
alias = aliasesEnum.nextElement();
Certificate[] certChain = keyStore.getCertificateChain(alias);
(...)
}

但不幸的是,我的链中只有一个证书(这张卡所有者的证书)。我没有任何受信任的根证书,因此在验证期间我收到一个错误,即文件是使用不受信任的证书签名的。

你有什么想法吗?我应该使用 SunPKSC11 类吗?它不适用于 java 7(我使用 java 6),看起来它已被弃用。有没有其他图书馆可以进入卡片的核心?

【问题讨论】:

    标签: java certificate pkcs#11


    【解决方案1】:

    实际上这张卡只包含一个证书,所以我的代码可以正常工作。我手动添加了缺少的证书并将它们连接到链中。有了这条链,我可以签署我的文件。我从另一个应用程序(proCertum 智能卡)保存证书丢失了证书,该应用程序正式用于使用这种类型的证书唱歌文件。

    【讨论】:

      【解决方案2】:

      我认为您的问题在 while 循环内:

      while (aliasesEnum.hasMoreElements()) {
      alias = aliasesEnum.nextElement();
      **Certificate[] certChain = keyStore.getCertificateChain(alias);**
      (...)
      }
      

      在这种情况下,您只会获得一个带有最后一个别名的证书。

      所以,我建议你把代码改成这样:

      Certificate[] certChain = new Certificate[NumnberOfYourCertificates];
      int count = 0 ;
      while (aliasesEnum.hasMoreElements()) {
      alias = aliasesEnum.nextElement();
      certChain[count++] = keyStore.getCertificateChain(alias);
      (...)
      }
      

      我认为它会起作用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-08-06
        • 2017-01-04
        • 2010-11-26
        • 1970-01-01
        • 2014-07-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多