gpt4 book ai didi

java - CMSSignedData 中的分离签名使用 Bouncy CaSTLe 进行验证,但不使用 java.security.Signature

转载 作者:行者123 更新时间:2023-11-30 06:04:53 30 4
gpt4 key购买 nike

几天来我一直在浏览网络以找到造成这种情况的原因,但没有成功。

我正在这样做:

  • 生成新证书(签名者证书和他的 parent )
  • 使用 CMSSignedDataGenerator(bouncycaSTLe 1.45)创建签名
  • 通过 java.security.Signature 验证 -> FAILS
  • 通过 BC 类验证 -> 成功

我发现的事实:

  • 签名的创建和验证使用相同的算法 - SHA1withRSA。
  • 过程中任何地方都没有抛出异常。

问题:

  • 知道我哪里做错了吗?

要考虑的想法:

我认为原因在于签名的生成,但我想我是根据文档来做的。或者 - 验证过程可能需要某种不同的结构,如 PKCS1,而生成器提供 PKCS7。实际上没有任何内容告诉我下一步要搜索什么。

这是一段代码,应该说明问题和输出(在提供您自己的证书和私钥之后):

import org.bouncycastle.cms.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.PrivateKey;
import java.security.Security;
import java.security.Signature;
import java.security.cert.CertStore;
import java.security.cert.CollectionCertStoreParameters;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import static org.bouncycastle.cms.CMSSignedGenerator.DIGEST_SHA1;
import static org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME;


public final class VerifyCMSSignedData {

private static final byte[] DATA_TO_BE_SIGNED = "data".getBytes();
private static final String SHA_1_WITH_RSA = "SHA1WithRSA";
private static final boolean DATA_NOT_ATTACHED = false;
private static final String COLLECTION_STORE_TYPE = "Collection";

private static X509Certificate signer;
private static PrivateKey signerPrivateKey;
private static X509Certificate parent;

private VerifyCMSSignedData() {
}

public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());

setUpCertificates();

shouldVerifySignature();
}

public static void shouldVerifySignature() throws Exception {
CMSSignedData signature = createSignature();

shouldVerifyBySignature(signature.getEncoded());

shouldVerifyByBC(signature.getEncoded());
}

private static void shouldVerifyByBC(byte[] signatureBytes) {
boolean verified = false;

try {
CMSSignedData cms = new CMSSignedData(new CMSProcessableByteArray(DATA_TO_BE_SIGNED), signatureBytes);
CertStore certStore = cms.getCertificatesAndCRLs(COLLECTION_STORE_TYPE, PROVIDER_NAME);
SignerInformationStore signers = cms.getSignerInfos();
Collection c = signers.getSigners();
for (Object aC : c) {
SignerInformation signer = (SignerInformation) aC;
Collection certCollection = certStore.getCertificates(signer.getSID());
Iterator certIt = certCollection.iterator();
X509Certificate cert = (X509Certificate) certIt.next();
verified = signer.verify(cert, PROVIDER_NAME);
}
} catch (Exception e) {
e.printStackTrace();
verified = false;
}

System.out.println(verified ? "VERIFIED BY BC" : "! Not verified through BC !");
}

private static void shouldVerifyBySignature(byte[] signatureBytes) throws Exception {
Signature signatureVerifier = Signature.getInstance(SHA_1_WITH_RSA, PROVIDER_NAME);
signatureVerifier.initVerify(signer.getPublicKey());
signatureVerifier.update(DATA_TO_BE_SIGNED);
boolean verified = signatureVerifier.verify(signatureBytes);

System.out.println(verified ? "VERIFIED BY SIGNATURE CLASS" : "! Not verified by Signature class !");
}

private static CMSSignedData createSignature() throws Exception {
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

gen.addSigner(signerPrivateKey, signer, DIGEST_SHA1);

List<X509Certificate> allCerts = new ArrayList<>();
if (parent != null) {
allCerts.add(parent);
}
allCerts.add(signer);

CertStore store = CertStore.getInstance(
COLLECTION_STORE_TYPE,
new CollectionCertStoreParameters(allCerts),
PROVIDER_NAME
);

gen.addCertificatesAndCRLs(store);

return gen.generate(new CMSProcessableByteArray(DATA_TO_BE_SIGNED), DATA_NOT_ATTACHED, PROVIDER_NAME);
}

private static void setUpCertificates() throws Exception {
// TODO setup your certificates here
}
}

输出:

! Not verified by Signature class !

VERIFIED BY BC

Process finished with exit code 0

最佳答案

您正在验证不同的事物。验证永远无法成功,因为 Java SHA1withRSA 验证需要 RSA PKCS#1_v15 签名,但您已生成 CMS 签名

CMS封装数字签名或加密消息以及一些附加元素,例如证书。您的 CMS 消息包含一个 PCKS#1 签名,但如果您想直接使用 Java Api 验证它,请注意,签名哈希不是根据要签名的数据计算的。它包含一些额外的元素,例如对 ASN.1 语法中签名证书的引用,因此您需要以相同的方式计算哈希

关于java - CMSSignedData 中的分离签名使用 Bouncy CaSTLe 进行验证,但不使用 java.security.Signature,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48319392/

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