gpt4 book ai didi

java - 使用 BouncyCaSTLe 将签名/认证属性添加到 CMS 签名

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:18:33 24 4
gpt4 key购买 nike

我想使用 bouncycaSTLe 生成一个简单的 CMS 签名。此代码有效!

  Security.addProvider(new BouncyCastleProvider());
String password = "123456";
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(new FileInputStream("c:/cert_123456.p12"), password.toCharArray());
String alias = (String)ks.aliases().nextElement();
PrivateKey key = (PrivateKey)ks.getKey(alias, password.toCharArray());
Certificate[] chain = ks.getCertificateChain(alias);

CMSSignedDataGenerator generator = new CMSSignedDataGenerator();

generator.addSigner(key, (X509Certificate)chain[0], CMSSignedDataGenerator.DIGEST_SHA1);
ArrayList list = new ArrayList();
for (int i = 0; i < chain.length; i++) {
list.add(chain[i]);
}
CertStore chainStore = CertStore.getInstance("Collection", new CollectionCertStoreParameters(list), "BC");
generator.addCertificatesAndCRLs(chainStore);
CMSProcessable content = new CMSProcessableByteArray("test".getBytes());
CMSSignedData signedData = generator.generate(content, false, "BC");

byte[] pk = signedData.getEncoded();

但是,如何添加签名属性呢?
我想删除默认签名属性并添加签名策略标识符。

文章非常受欢迎。

最佳答案

首先,您使用的结构似乎在最新版本的 Bouncy CaSTLe 中已弃用。添加经过身份验证/签名的 attributes你必须把它们打包成一个 AttributeTable已签名的属性像这样添加到签名者:

ASN1EncodableVector signedAttributes = new ASN1EncodableVector();
signedAttributes.add(new Attribute(CMSAttributes.contentType, new DERSet(new ASN1ObjectIdentifier("1.2.840.113549.1.7.1"))));
signedAttributes.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(digestBytes))));
signedAttributes.add(new Attribute(CMSAttributes.signingTime, new DERSet(new DERUTCTime(signingDate))));

AttributeTable signedAttributesTable = new AttributeTable(signedAttributes);

然后在其中一个 addSigner 方法中使用它。正如我在开头提到的,此方法已被弃用,我们鼓励您使用生成器和生成器生成器。这是一个简短的例子:

    /* Construct signed attributes */
ASN1EncodableVector signedAttributes = new ASN1EncodableVector();
signedAttributes.add(new Attribute(CMSAttributes.contentType, new DERSet(new ASN1ObjectIdentifier("1.2.840.113549.1.7.1"))));
signedAttributes.add(new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(digestBytes))));
signedAttributes.add(new Attribute(CMSAttributes.signingTime, new DERSet(new DERUTCTime(signingDate))));

AttributeTable signedAttributesTable = new AttributeTable(signedAttributes);
signedAttributesTable.toASN1EncodableVector();
DefaultSignedAttributeTableGenerator signedAttributeGenerator = new DefaultSignedAttributeTableGenerator(signedAttributesTable);

/* Build the SignerInfo generator builder, that will build the generator... that will generate the SignerInformation... */
SignerInfoGeneratorBuilder signerInfoBuilder = new SignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build());
signerInfoBuilder.setSignedAttributeGenerator(signedAttributeGenerator);
CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
JcaContentSignerBuilder contentSigner = new JcaContentSignerBuilder("SHA1withRSA");
contentSigner.setProvider("BC");

generator.addSignerInfoGenerator(signerInfoBuilder.build(contentSigner.build(this.signingKey), new X509CertificateHolder(this.signingCert.getEncoded())));

ArrayList<X509CertificateHolder> signingChainHolder = new ArrayList<X509CertificateHolder>();
Iterator i = this.signingChain.iterator();
while (i.hasNext()) {
X509CertificateObject cert = (X509CertificateObject)i.next();
signingChainHolder.add(new X509CertificateHolder(cert.getEncoded()));
}

generator.addCertificates(new JcaCertStore(signingChainHolder));
generator.generate(new CMSAbsentContent(), "BC").getEncoded();

它非常笨重,可能还没有用(我正在写它,在研究一些东西时偶然发现了你的问题),尤其是 signingDate 部分,它可能必须是 new DERSet( new Time(new Date))(更新:它适用于 DERUTCTime)。

有点离题:我仍然无法理解 Signed 和 Authenticated 属性之间的区别,Bouncy CaSTLe 有 DefaultAuthenticatedAttributeTableGenerator 和 DefaultSignedAttributeTableGenerator 类,它们与 Signers 完美配合。两者在 signingTime 方面似乎存在一些细微差别,如果不存在,SignedAttributes 默认会添加 signingTime。 RFC 提到了这两种属性类型,但我找不到任何明确的东西。

关于java - 使用 BouncyCaSTLe 将签名/认证属性添加到 CMS 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10424968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com