java使用bouncycastle和SunMSCAPI创建外部(2步)签名
我试图从应用程序中删除一些不推荐的BC代码,但切换旧的CMSSignedDataGenerator时遇到问题。将签名者添加到新的API
在Linux(PKCS11提供程序)和Windows(SunMSCAPI)上运行的旧代码是这样的:
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
CertStore certStore = CertStore.getInstance("Collection",new CollectionCertStoreParameters(Arrays.asList(certChain)));
Attribute attrHash = new Attribute(CMSAttributes.messageDigest, new DERSet( new DEROctetString(hash) ) );
Attribute attrcontentType = new Attribute(CMSAttributes.contentType, new DERSet( CMSObjectIdentifiers.data ) );
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(attrHash);
v.add(attrcontentType);
gen.addSigner(aPrivateKey, userCertificate, CMSSignedDataGenerator.DIGEST_SHA1, new AttributeTable(v), null);
gen.addCertificatesAndCRLs(certStore);
CMSSignedData s = gen.generate(CMSSignedDataGenerator.DATA, null, false, getProviderName() );
return s.getEncoded();
提供者名称可以是“SunMSCAPI”,也可以是我们在Linux上为PKCS#11动态使用的任何名称
addSigner和generate方法在BC中都被弃用,所以现在我做以下工作:
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
Store certStore = new JcaCertStore(Arrays.asList(certChain));
Attribute attrHash = new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(hash)));
Attribute attrcontentType = new Attribute(CMSAttributes.contentType, new DERSet(CMSObjectIdentifiers.data));
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(attrHash);
v.add(attrcontentType);
ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider(provider).build(aPrivateKey);
gen.addSignerInfoGenerator(
new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider(provider).build())
.setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator(new AttributeTable(v)))
.build(sha1Signer, userCertificate));
gen.addCertificates(certStore);
CMSSignedData s = gen.generate(new CMSAbsentContent(), false);
return s.getEncoded();
这段新代码的问题是,当使用“BC”作为提供程序时,它可以完美地工作,但当我切换到“SunMSCAPI”时,会出现以下异常:
no such algorithm: SHA-1 for provider SunMSCAPI
这种复杂的签名方式只是因为我们需要签名摘要,我们没有完整的内容可供签名,所以我必须将预先计算的摘要传递给签名方法
有人能帮我找出上面的代码可能有什么问题吗
提前谢谢
共 (0) 个答案