【发布时间】:2018-06-03 15:45:10
【问题描述】:
我需要使用 bouncy castle 1.58 生成 p7b 证书链。
在我们使用的旧版本(1.46)中,此代码有效:
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
Certificate [] chain = certificate.getCertificateChain();
CertStore certStore;
try {
certStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(Arrays.asList(chain)));
gen.addCertificatesAndCRLs(certStore);
CMSSignedData signedData = gen.generate(null,(Provider)null);
return signedData.getEncoded();
} catch (Exception ex) {
logger.error("Failed to construct P7B response",ex);
throw new RuntimeException(ex);
}
但是,新版本的 Bouncy Castle 对 CMSSignedDataGenerator 进行了一些更改,因此我将代码修改如下:
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
Certificate [] chain = certificate.getCertificateChain();
try {
JcaCertStore store = new JcaCertStore(Arrays.asList(chain));
gen.addCertificates(store);
CMSSignedData signedData = gen.generate(null);
return signedData.getEncoded();
} catch (Exception ex) {
logger.error("Failed to construct P7B response",ex);
throw new RuntimeException(ex);
}
但是,我在生成中的这一行上得到一个空指针异常:
CMSSignedData signedData = gen.generate(null);
我尝试调试并检查证书是否已加载到 JcaCertStore,所以该部分没问题。
但是,当我尝试调试充气城堡库时,调试器似乎无法找到 CMSSignedDataGenerator 类的行号。
我正在使用 Wildfly 部署我的项目,并且我已将带有源的 jar 附加到调试器,但是我看到了代码,但在类名旁边我得到 line not available,所以我看不到空指针异常发生在哪里。
【问题讨论】:
标签: java cryptography certificate bouncycastle pki