在 Java 密钥库中导入 pkcs12 (.p12) 客户端证书文件时,CertificateChain 为空

问题描述

我将带有证书链的 client-ssl-certificate KeyPair 导出为 PKCS12 文件密钥库资源管理器。我可以再次使用 KSE 加载这个 PKCS12 文件,并且密钥对和证书链都在那里。当我将它加载到 Java KeyStore 中时,我可以访问该密钥,但证书链为空。

这是我的代码

        final KeyStore instance = KeyStore.getInstance( "pkcs12" );
        instance.load( new ByteArrayInputStream( bytes ),password );

        instance.getKey(alias,password) => returns Key
        instance.getCertificateChain(alias) => returns null

        final KeyStore.ProtectionParameter param = new KeyStore.PasswordProtection( password );
        final KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) instance.getEntry( alias,param );

        => fails:

java.lang.NullPointerException: invalid null input
    at java.security.KeyStore$PrivateKeyEntry.<init>(KeyStore.java:524) ~[na:1.8.0_202]
    at sun.security.pkcs12.PKCS12KeyStore.engineGetEntry(PKCS12KeyStore.java:1311) ~[na:1.8.0_202]
    at java.security.KeyStore.getEntry(KeyStore.java:1521) ~[na:1.8.0_202]

通过调试我可以看到,PrivateKeyEntry 的构造函数是在链参数为空的情况下调用的:

image: debugging PrivateKeyEntry constructor call

我对此完全没有解释,也没有在互联网上找到任何信息。

我可以排除使用的别名和使用的密码作为原因。

我会感谢有关此主题的任何提示

提前致谢,

亚历山大

解决方法

正如你在这里看到的:

https://github.com/frohoff/jdk8u-jdk/blob/master/src/share/classes/java/security/KeyStore.java#L523

在以下三种情况之一中抛出异常:

  1. 您有密钥但没有私钥。
  2. 你没有链条(你就是这么说的)。
  3. 您没有任何属性。

要确保链是问题,您可以使用

激活调试
    series: [{
        showInLegend: true,allowPointSelect: true,type: 'pie',data: [{
            y: 45,color: 'black',dataLabels: {
                enabled: false
            }
        },{
            y: 55,color: 'blue',sliced: true,borderWidth: 5,borderColor: 'white',dataLabels: {
                format: '{point.y}%',distance: -60,style: {
                    fontSize: 24
                }
            }
        }]
    }]