我正在使用Java实现C#SignedCms功能。
我有一个pkcs7 SignedData (请参阅我的附件:https://www.dropbox.com/s/yivani7dvh98wpa/SignedData.bin?dl=0),它可以在C#中验证:
//signed data is loaded from my attached file.
bool VerifyPKCS7(byte[] signedData)
{
try
{
SignedCms signedCms = new SignedCms();
signedCms.Decode(signedData);
signedCms.CheckSignature(true);
return true;
}
catch
{
}
return false;
}
但它无法使用Java中的Bouncy Castle库(bcprov-jdk15on-153.jar,bcpkix-jdk15on-153.jar)进行验证:
//encapSigData is loaded from my attached file.
CMSSignedDataParser sp = new CMSSignedDataParser(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build(), encapSigData);
sp.getSignedContent().drain();
Store certStore = sp.getCertificates();
SignerInformationStore signers = sp.getSignerInfos();
Collection c = signers.getSigners();
Iterator it = c.iterator();
while (it.hasNext())
{
SignerInformation signer = (SignerInformation)it.next();
Collection certCollection = certStore.getMatches(signer.getSID());
Iterator certIt = certCollection.iterator();
X509CertificateHolder cert = (X509CertificateHolder)certIt.next();
System.out.println("verify returns: " + signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider("BC").build(cert)));
}
我在第一个代码行(CMSSignedDataParser
构造函数)中遇到异常:
java.lang.ClassCastException: org.bouncycastle.asn1.DERSequenceParser cannot be cast to org.bouncycastle.asn1.ASN1OctetStringParser
at org.bouncycastle.cms.CMSSignedDataParser.<init>(Unknown Source)
at org.bouncycastle.cms.CMSSignedDataParser.<init>(Unknown Source)
at org.bouncycastle.cms.CMSSignedDataParser.<init>(Unknown Source)
经过一些分析后,我发现 SignedData 中 contentInfo 的内容是一个序列。似乎bouncycastle不能接受序列作为内容。
如何在Java中使用bouncycastle验证 SignedData ?
答案 0 :(得分:1)
这里的问题是,与常规CMS消息不同,这实际上是PKCS7消息。现在已经在Bouncy Castle的bcpkix API中添加了对这些的支持。
您可以在http://www.bouncycastle.org/betas 154b12或更高版本的最新测试版中找到它。